How does LINQ's “Where” method works?

后端 未结 3 2000
无人及你
无人及你 2021-01-21 10:24

Exactly how LINQ\'s \"where\" method is defined? I\'m guessing implementation is something like this:

public static IEnumerable Where ( this partialPare         


        
3条回答
  •  生来不讨喜
    2021-01-21 10:49

    The following "pseudo"-code blocks give the same code:

    // first
    from item in sequence
    where expression
    select item;
    
    // second
    sequence.Where(item => expression);
    

    It does not matter how many comparisons that are made in expression as long as it yields a boolean result. It's still one expression.

    This means that the following two are also identical:

    // first
    from c in context.Con
    where ( c.Col1 == c.Col2 || c.Col3 == c.Col4 )
    select c;
    
    // second
    context.Con.Where(c => c.Col1 == c.Col2 || c.Col3 == c.Col4);
    

提交回复
热议问题