如何编写linq查询来访问多个表中的数据。如何为以下sql查询编写Linq查询: -
"select * from user,employee where user.Name='smith' and employee.company='xyz'"
答案 0 :(得分:1)
这样的事情就可以做到。
var q = from u in db.user
from e in db.employee
where u.Name == "smith" && e.company == "xyz"
select new
{
User = u,
Employee = e
};
答案 1 :(得分:0)
这取决于您使用的linq提供商。例如,假设您正确定义了外键,实体框架会创建他们称之为“导航属性”的内容。因此,您可以轻松地以这种方式编写如上所述的linq查询:
var query = data.Where(employee => employee.Name == "Smith" && employee.Company.Name == "xyz");
答案 2 :(得分:0)
var result = from u in context.User
from e in context.Employee
where (u.Name == "smith" && e.company == "xyz")