简体   繁体   English

仅具有一个匹配属性的MoreLinq ExceptBy

[英]MoreLinq ExceptBy with only one matching property

I have 2 different items that I'm trying to perform the except by method using the more linq library. 我尝试使用2个不同的项目,除了使用更多linq库的方法。 The only thing that is common between both items is a single string called Symbol. 两项之间唯一的共同点是一个称为Symbol的字符串。 Item 1 is a list of tuples and Item 2 is a database table that I open with linq 项目1是元组列表,项目2是我用linq打开的数据库表

Item 1: 项目1:

Tuple<string Symbol, string Market, List<Class1>, List<Class2>>

Item 2: 项目2:

List<TableClass>

TableClass {
string Symbol;
decimal Price;
bool isSold; }

Here is the code I attempted. 这是我尝试的代码。 I'm trying to return all elements from Item1 that don't exist in Item2: 我正在尝试从Item1返回所有在Item2中不存在的元素:

Item1.ExceptBy(Item2.Select(k => k.Symbol), e => e.Symbol);

This doesn't work because Item1 and Item2 are completely different types 这不起作用,因为Item1和Item2是完全不同的类型

The type arguments for method 'MoreEnumerable.ExceptBy(IEnumerable, IEnumerable, Func)' cannot be inferred from the usage. 无法从用法中推断方法'MoreEnumerable.ExceptBy(IEnumerable,IEnumerable,Func)'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

I keep an extension method handy for doing left outer joins: 我保留了方便的扩展方法来进行左外部联接:

public static IEnumerable<TResult> LeftOuterJoin<TLeft, TRight, TKey, TResult>(
    this IEnumerable<TLeft> leftSeq,
    IEnumerable<TRight> rightSeq,
    Func<TLeft, TKey> keySelectorLeft,
    Func<TRight, TKey> keySelectorRight,
    Func<TLeft, TRight, TResult> projectionSelector)
{
    return leftSeq
        .GroupJoin(
            rightSeq,
            keySelectorLeft,
            keySelectorRight,
            (leftItem, rightItems) => new { leftItem, rightItems })
        .SelectMany(
            x => x.rightItems.DefaultIfEmpty(),
            (x, rightItem) => projectionSelector(x.leftItem, rightItem));
}

So now we can: 现在我们可以:

tuples.LeftOuterJoin(list, t => t.Symbol, k => k.Symbol, (t,k) => (t,k))
      .Where(x => x.k == null)
      .Select(x => x.t)

...and this will give us all the items from the tuple list with a Symbol property that does not have a corresponding item in the other list with the same Symbol . ...这将为我们提供元组列表中具有Symbol属性的所有项目,而在其他列表中没有具有相同Symbol Right? 对?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM