Help Understanding Enumerable.Join Method

前端 未结 4 686
时光取名叫无心
时光取名叫无心 2021-02-04 11:45

Yesterday I posted this question regarding using lambdas inside of a Join() method to check if 2 conditions exist across 2 entities. I received an answer on the question, which

相关标签:
4条回答
  • 2021-02-04 12:03

    Eric and Nick have both provided good answers.

    You may also write the Linq query expression using query syntax (vs. method syntax, which you are using in your example):

    var query = from a in db.TableA 
                join b in db.TableB on a.someFieldID equals b.someFieldID
                where a.UserID == currentUser && b.MyField == someValue
                select a;
    
            if (query.Any()) {
                ...
            }
    

    Update:

    You seem to be stuck on the lambda expressions. It's a function that you pass around like a variable. A lambda expression is equivalent to an anonymous delegate (or anonymous method, to me more general).

    Here is your query with the lambda expressions as delegates (replace EntityType with the type of your entity returned from TableA, of course):

    if (db.TableA.Where( delegate(EntityType a) { return a.UserID == currentUser; } ) 
      .Join( db.TableB.Where( delegate(EntityType b) { return b.MyField == someValue; } ), 
             delegate(EntityType o) { return o.somefieldId); },
             delegate(EntityType i) { return i.someFieldId); },
             delegate(EntityType o, EntityType i) { return o; }) 
      .Any())  
    

    { //... }

    NOTE: A lambda expression has important aspects that make it more than just an equivalent for anonymous methods. I recommend that you look through other SO questions and read online about lambda expressions in particular. They allow for very powerful ideas to be expressed in a much simpler and elegant way. It's a deep topic, but the basics are easy enough to understand. It's a function that you can pass around like a variable, or as a parameter to other functions.

    0 讨论(0)
  • 2021-02-04 12:03

    This query is saying join TableA to TableB where TableA.someFieldID == TableB.someFieldID and selecting the results from TableA and seeing if there are any results at all

    In terms of SQL think of it like this, even if it's not Linq-to-SQL...if you are familiar with SQL maybe this makes more sense:

    Select Count(*)
    From TableA a
         Join TableB b
           On a.someFieldID = b.someFieldID
    

    Then checking if Count(*) is > 0

    0 讨论(0)
  • 2021-02-04 12:19

    Explanation of the Join.

    b = object type of first table o = object type of first table i = object type of second table

    1. db.TableB.Where( b => b.MyField == someValue ) This is the element type of the second table
    2. o => o.someFieldID The key of the first table
    3. i => i.someFieldID The key of the second table (which will match the key in the first table)
    4. (o,i) => o The object to return, in this case the object type of the first table.
    0 讨论(0)
  • 2021-02-04 12:22

    The join syntax is

    FirstTable.Join(SecondTable, FirstTableKeyExtractor, SecondTableKeyExtractor, Selector)
    

    So you have two tables. You have some key that is common to both tables. You provide two key extractors that know how to get the key out of each row in the table.

    The join logic identifies pairs of rows, one from each table, that have the same key.

    Each of those rows is then run through the selector to project the result.

    Does that answer your question?

    0 讨论(0)
提交回复
热议问题