【问题标题】:How to solve the error cannot be inferred from the usage. Try specifying the type arguments explicitly?从用法无法推断如何解决错误。尝试明确指定类型参数?
【发布时间】:2016-09-12 02:55:04
【问题描述】:
static Func<T, object> CreateSelector<T>(IEnumerable<string> propertyNames)
{
    var sourceType = typeof(T);
    var parameter = Expression.Parameter(sourceType, "e");
    var properties = propertyNames.Select(name => Expression.PropertyOrField(parameter, name)).ToArray();
    var selector = Expression.Lambda<Func<T, object>>(
        Expression.Call(typeof(Tuple), "Create", properties.Select(p => p.Type).ToArray(), properties),
        parameter);
    return selector.Compile();
}
public static IEnumerable<Tuple<T, T>> Join<T>(this IEnumerable<T> left, IEnumerable<T> right, IEnumerable<string> propertyNames)
{
    var keySelector = CreateSelector<T>(propertyNames);
    return left.Join(right, keySelector, keySelector, Tuple.Create);
}
public static bool CompareLists<T1, T2>(IEnumerable<T1> lstProduct1, IEnumerable<T2> lstProduct2, List<DuplicateExpression> DuplicateExpression)
{
    string[] Fields = DuplicateExpression.Select(x => x.ExpressionName).ToArray();
    var JoinExp = lstProduct1.Join(lstProduct2, Fields);
    bool IsSuccess = true;// CompareTwoLists(lstProduct1, lstProduct2, (listProductx1, listProductx2) => JoinExp.Any());

    return IsSuccess;
}

当我编译上面的代码时;我在下面的行中遇到错误

var JoinExp = lstProduct1.Join(lstProduct2, Fields);

错误是

错误 1 ​​方法的类型参数 'AP.Classes.ListComparison.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable)' 无法推断 从用法上。尝试指定类型参数 明确地。 D:\Workshop\Ap\Classes\DuplicateValidator.cs

如何解决这个错误?我正在创建一个列表比较工具。

【问题讨论】:

  • 看看你的 Join 方法 - 它需要 one 类型的参数。它希望您的两个序列属于同一类型。您的 CompareLists 方法允许两个序列属于 不同 类型。你想达到哪个目标?
  • bool IsSuccess =CompareTwoLists(lstProduct1, lstProduct2, (listProductx1, listProductx2) =&gt; JoinExp.Any()); 我正在将这两个lists 传递给具有该连接的另一个函数。两个列表具有相同的属性。
  • 你错过了我的意思。在您的CompareLists 方法中,lstProduct1lstProduct2 可以是不同的类型——例如,您可以调用CompareLists(new List&lt;string&gt;(), new List&lt;int&gt;(), null);。但是在CompareLists 中,您调用的是Join,它需要两个相同 元素类型的序列...两个参数都是IEnumerable&lt;T&gt;。现在,您希望能够连接具有不同元素类型的序列吗?如果是这样,请更改Join。如果没有,请更改CompareLists
  • 我是自定义 Join 方法的作者(作为对您之前问题的回答),我应该说假设您要加入两个相同类型的列表。如果它们不同,则该方法应该具有不同的签名和实现。由于您针对同一主题发布了多个问题,因此如果您提供您想要实现的目标的示例,而不是实施尝试,那就太好了。
  • 您是否正在寻找像public static bool CompareTwoLists&lt;T1, T2&gt;(IEnumerable&lt;T1&gt; list1, IEnumerable&lt;T2&gt; list2, Enumerable&lt;string&gt; propertyNames) 这样的方法,如果基于指定的属性有任何差异,则返回true

标签: c# linq


【解决方案1】:

提供的自定义 Join 方法不适用,因为它需要一个泛型类型参数,而您的方法有两个。

您可以使用提供的CreateSelector 实现来实现自定义Join / GroupJoin 扩展方法,类似于Enumerable 类中相应的系统提供的方法,如下所示:

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);
}

那么你就可以使用上面自定义的GroupJoin来高效的实现你的方法了:

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());
}

【讨论】:

  • var fields = DuplicateExpression.Select(x =&gt; x.ExpressionName).ToArray(); return list1.GroupJoin(list2, fields, (x, match) =&gt; match).All(match =&gt; match.Any()) &amp;&amp; list2.GroupJoin(list1, fields, (x, match) =&gt; match).All(match =&gt; match.Any()); 这没有得到正确的结果
  • 如果任何一项在其他列表中重复;它返回假。如果所有项目都重复,则返回 true
  • Product p1 = new Product { ProductCode = "1", ProductName = "ABC", ProductType = "Doe", Amount = "500" }; Product p2 = new Product { ProductCode = "2", ProductName = "DEF", ProductType = "Doe", Amount = "500" }; Product p3 = new Product { 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
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多