What is the syntax for an inner join in LINQ to SQL?

后端 未结 19 1632
感情败类
感情败类 2020-11-22 04:25

I\'m writing a LINQ to SQL statement, and I\'m after the standard syntax for a normal inner join with an ON clause in C#.

How do you represent the follo

相关标签:
19条回答
  • 2020-11-22 04:59

    basically LINQ join operator provides no benefit for SQL. I.e. the following query

    var r = from dealer in db.Dealers
       from contact in db.DealerContact
       where dealer.DealerID == contact.DealerID
       select dealerContact;
    

    will result in INNER JOIN in SQL

    join is useful for IEnumerable<> because it is more efficient:

    from contact in db.DealerContact  
    

    clause would be re-executed for every dealer But for IQueryable<> it is not the case. Also join is less flexible.

    0 讨论(0)
  • 2020-11-22 04:59
    var q=(from pd in dataContext.tblProducts join od in dataContext.tblOrders on pd.ProductID equals od.ProductID orderby od.OrderID select new { od.OrderID,
     pd.ProductID,
     pd.Name,
     pd.UnitPrice,
     od.Quantity,
     od.Price,
     }).ToList(); 
    
    0 讨论(0)
  • 2020-11-22 05:00

    Use Linq Join operator:

    var q =  from d in Dealer
             join dc in DealerConact on d.DealerID equals dc.DealerID
             select dc;
    
    0 讨论(0)
  • 2020-11-22 05:03

    To extend the expression chain syntax answer by Clever Human:

    If you wanted to do things (like filter or select) on fields from both tables being joined together -- instead on just one of those two tables -- you could create a new object in the lambda expression of the final parameter to the Join method incorporating both of those tables, for example:

    var dealerInfo = DealerContact.Join(Dealer, 
                                  dc => dc.DealerId,
                                  d => d.DealerId,
                                  (dc, d) => new { DealerContact = dc, Dealer = d })
                              .Where(dc_d => dc_d.Dealer.FirstName == "Glenn" 
                                  && dc_d.DealerContact.City == "Chicago")
                              .Select(dc_d => new {
                                  dc_d.Dealer.DealerID,
                                  dc_d.Dealer.FirstName,
                                  dc_d.Dealer.LastName,
                                  dc_d.DealerContact.City,
                                  dc_d.DealerContact.State });
    

    The interesting part is the lambda expression in line 4 of that example:

    (dc, d) => new { DealerContact = dc, Dealer = d }
    

    ...where we construct a new anonymous-type object which has as properties the DealerContact and Dealer records, along with all of their fields.

    We can then use fields from those records as we filter and select the results, as demonstrated by the remainder of the example, which uses dc_d as a name for the anonymous object we built which has both the DealerContact and Dealer records as its properties.

    0 讨论(0)
  • 2020-11-22 05:03
    var data=(from t in db.your tableName(t1) 
              join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname
              (where condtion)).tolist();
    
    0 讨论(0)
  • 2020-11-22 05:04
    var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID
    select new{
    dealer.Id,
    dealercontact.ContactName
    
    }).ToList();
    
    0 讨论(0)
提交回复
热议问题