简体   繁体   中英

how to do a Join in Entity Framework

Instead of writing a linq query, is there a way I can just do a join by simply doing something like this:

using (var db = new MatchGamingEntities())
{
    db.Accounts.Join() //I am unsure of the syntax
    db.Accounts.Include...
    ...

    return View(Account.SingleOrDefault());
}

I want to use these predefined Entity functions instead of writing linq, is it practical? Also how do you use these predefined functions? I have a table called "Accounts" and "BankTransactions" they both have AccountId in common, How would I query that using these functions and what type of result would it return its one to many relationship.

LINQ is really your better bet, things start to get hectic in Lambda's really fast and linq just looks a lot better given the structured way compared to lambda's

See this SO Post:

C# Joins/Where with Linq and Lambda

var query = db.Accounts.Join(db.BankTransactions,
     acc => acc.AccountID,
     bank => bank.AccountID,
     (acc,bank) => new { Account = acc, BankTransaction = bank });

Edit: This should(or something similar) return a query that will return a collection of Accounts and inside each account it's relevant BankTransaction.

This should do it but again, rather use LINQ if possible.

Edit: Just as an afterthought, you can add additional lamba extensions like a where clause to the above one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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