使用LINQ多个表中选择 [英] Select from multiple table using LINQ
问题描述
该查询的伟大工程:
var pageObject = (from op in db.ObjectPermissions
join pg in db.Pages on op.ObjectPermissionName equals page.PageName
where pg.PageID == page.PageID
select op)
.SingleOrDefault();
我得到一个新的类型与我的'运'领域。现在我想找回我的PG字段为好,但
I get a new type with my 'op' fields. Now I want to retrieve my 'pg' fields as well, but
select op, pg).SingleOrDefault();
不起作用。
doesn't work.
如何我可以从两个表中选择的一切,让他们出现在我的新pageObject类型?
How can I select everything from both tables so that they appear in my new pageObject type?
推荐答案
您可以使用匿名类型对于这一点,即:
You can use anonymous types for this, i.e.:
var pageObject = (from op in db.ObjectPermissions
join pg in db.Pages on op.ObjectPermissionName equals page.PageName
where pg.PageID == page.PageID
select new { pg, op }).SingleOrDefault();
这将使pageObject成一个匿名类型的IEnumerable所以据我所知,你将无法通过它周围的其他方法,但是如果你只是获得数据与方法你目前是完全没有发挥。您也可以命名你的匿名类型,即属性: - 在db.ObjectPermissions $ B $
This will make pageObject into an IEnumerable of an anonymous type so AFAIK you won't be able to pass it around to other methods, however if you're simply obtaining data to play with in the method you're currently in it's perfectly fine. You can also name properties in your anonymous type, i.e.:-
var pageObject = (from op in db.ObjectPermissions
join pg in db.Pages on op.ObjectPermissionName equals page.PageName
where pg.PageID == page.PageID
select new
{
PermissionName = pg,
ObjectPermission = op
}).SingleOrDefault();
这将使你说: -
if (pageObject.PermissionName.FooBar == "golden goose") Application.Exit();
例如: - )
这篇关于使用LINQ多个表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!