Entityframework Join using join method and lambdas

前端 未结 3 1566
无人及你
无人及你 2020-12-04 12:50

It seems there are different ways to do joins using linq. One is more straightforward and involves just joining tables like this:

var found = from c in s.cat         


        
3条回答
  •  独厮守ぢ
    2020-12-04 13:31

    You can find a few examples here:

    // Fill the DataSet.
    DataSet ds = new DataSet();
    ds.Locale = CultureInfo.InvariantCulture;
    FillDataSet(ds);
    
    DataTable contacts = ds.Tables["Contact"];
    DataTable orders = ds.Tables["SalesOrderHeader"];
    
    var query =
        contacts.AsEnumerable().Join(orders.AsEnumerable(),
        order => order.Field("ContactID"),
        contact => contact.Field("ContactID"),
        (contact, order) => new
        {
            ContactID = contact.Field("ContactID"),
            SalesOrderID = order.Field("SalesOrderID"),
            FirstName = contact.Field("FirstName"),
            Lastname = contact.Field("Lastname"),
            TotalDue = order.Field("TotalDue")
        });
    
    
    foreach (var contact_order in query)
    {
        Console.WriteLine("ContactID: {0} "
                        + "SalesOrderID: {1} "
                        + "FirstName: {2} "
                        + "Lastname: {3} "
                        + "TotalDue: {4}",
            contact_order.ContactID,
            contact_order.SalesOrderID,
            contact_order.FirstName,
            contact_order.Lastname,
            contact_order.TotalDue);
    }
    

    Or just google for 'linq join method syntax'.

提交回复
热议问题