anthonypliu anthonypliu - 2 months ago 10
C# Question

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.

Answer

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,
     Account => 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