C# 无法从用法推断如何解决错误。是否尝试显式指定类型参数?
错误是 方法的类型参数出现错误1 'AP.Classes.ListComparison.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, 无法推断System.Collections.Generic.IEnumerable)' 从用法来看。尝试指定类型参数 明确地D:\Workshop\Ap\Classes\DuplicateValidator.csC# 无法从用法推断如何解决错误。是否尝试显式指定类型参数?,c#,linq,C#,Linq,错误是 方法的类型参数出现错误1 'AP.Classes.ListComparison.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, 无法推断System.Collections.Generic.IEnumerable)' 从用法来看。尝试指定类型参数 明确地D:\Workshop\Ap\Classes\DuplicateValidator.cs 如何解决这个错误?我正在创
如何解决这个错误?我正在创建一个列表比较工具。提供的自定义
Join
方法不适用,因为它需要一个泛型类型参数,而您的方法需要两个泛型类型参数
您可以使用提供的CreateSelector
实现来实现自定义Join
/GroupJoin
扩展方法,类似于可枚举
类中相应系统提供的方法,如下所示:
var JoinExp = lstProduct1.Join(lstProduct2, Fields);
我们来看一下您的
Join
方法-它需要一个类型参数。它希望您的两个序列属于同一类型。您的CompareLists
方法允许两个序列具有不同的类型。您试图实现哪一个目标?bool issucess=comparetwoolists(lstProduct1,lstProduct2,(listProductx1,listProductx2)=>JoinExp.Any())
我正在将这两个列表
传递给具有该联接的另一个函数。两个列表都有相同的属性。你没有领会我的意思。在CompareLists
方法中,lstProduct1
和lstProduct2
可以是不同的类型-例如,可以调用CompareLists(new List(),new List(),null)代码>。但是在compareList
中,您正在调用Join
,它需要两个相同元素类型的序列。。。这两个参数都是IEnumerable
。现在,您是否希望能够连接具有不同元素类型的序列?如果是,请更改Join
。如果没有,请更改CompareLists
。我是自定义Join
方法的作者(作为对您前面问题的回答),我应该说,假设您正在加入两个相同类型的列表。如果它们不同,则该方法应具有不同的签名和实现。由于您在同一主题上发布了多个问题,如果您提供了您想要实现的具体内容的示例,而不是实现尝试,那就太好了。您是否在寻找类似public static bool comparetwoolists(IEnumerable list1、IEnumerable list2、Enumerable propertyNames)的方法
如果基于指定属性存在任何差异,则返回true
?var fields=DuplicateExpression.Select(x=>x.ExpressionName).ToArray();返回list1.GroupJoin(list2,fields,(x,match)=>match.All(match=>match.Any())和&list2.GroupJoin(list1,fields,(x,match)=>match.All(match=>match.Any())代码>如果其他列表中的任何一项重复,则无法获得正确的结果;它返回false。如果所有项目都重复,则返回trueProduct p1=新产品{ProductCode=“1”,ProductName=“ABC”,ProductType=“Doe”,Amount=“500”};产品p2=新产品{ProductCode=“2”,ProductName=“DEF”,ProductType=“Doe”,Amount=“500”};产品p3=新产品{ProductCode=“3”,ProductName=“GHI”,ProductType=“Doe”,Amount=“500”}代码>产品q1=新产品{ProductCode=“1”,ProductName=“ABC”,ProductType=“Doe”,Amount=“5001”};产品q2=新产品{ProductCode=“21”,ProductName=“DEF1”,ProductType=“Doe1”,Amount=“5001”};产品q3=新产品{ProductCode=“31”,ProductName=“GHI1”,ProductType=“Doe1”,Amount=“5001”};以上P1和Q1重复项。表达式字段包括ProductCode、ProductName、ProductType
var JoinExp = lstProduct1.Join(lstProduct2, Fields);
public static IEnumerable<TResult> Join<TOuter, TInner, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, IEnumerable<string> propertyNames, Func<TOuter, TInner, TResult> resultSelector)
{
return outer.Join(inner, CreateSelector<TOuter>(propertyNames), CreateSelector<TInner>(propertyNames), resultSelector);
}
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, IEnumerable<string> propertyNames, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
{
return outer.GroupJoin(inner, CreateSelector<TOuter>(propertyNames), CreateSelector<TInner>(propertyNames), resultSelector);
}
public static bool CompareLists<T1, T2>(IEnumerable<T1> list1, IEnumerable<T2> list2, List<DuplicateExpression> DuplicateExpression)
{
var fields = DuplicateExpression.Select(x => x.ExpressionName).ToArray();
return list1.GroupJoin(list2, fields, (x, match) => match).All(match => match.Any())
&& list2.GroupJoin(list1, fields, (x, match) => match).All(match => match.Any());
}