Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 460
anthonypliu Avatar asked Mar 18 '11 05:03

anthonypliu


1 Answers

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.

like image 105
TBohnen.jnr Avatar answered Nov 02 '22 15:11

TBohnen.jnr