如何在实体框架中进行联接 [英] how to do a Join in Entity Framework

查看:74
本文介绍了如何在实体框架中进行联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代替编写linq查询,有没有一种方法可以通过简单地执行以下操作来进行联接:

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());
}

我想使用这些预定义的Entity函数而不是编写linq,是否可行?另外,您如何使用这些预定义功能?我有一个名为"Accounts"和"BankTransactions"的表,它们都具有共同的AccountId,我将如何使用这些函数查询该结果以及它将返回其一对多关系的结果是什么类型.

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确实是您最好的选择,在Lambda的快速发展中,事情开始变得忙碌起来,并且与lambda的相比,鉴于结构化方式,linq看起来要好得多

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

请参阅此SO Post:

See this SO Post:

C#在Linq和Lambda中加入/在这里

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

这应该(或类似的东西)返回一个查询,该查询将返回一个帐户集合,并且在每个帐户内与之相关的BankTransaction.

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

应该这样做,但还是要这样做,如果可能的话,请使用LINQ.

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

就像事后的想法一样,您可以在上述位置添加其他lamba扩展,例如where子句.

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

这篇关于如何在实体框架中进行联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆