C# 从多个表中创建 LINQ 选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a LINQ select from multiple tables
提问by ScottG
This query works great:
此查询效果很好:
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();
I get a new type with my 'op' fields. Now I want to retrieve my 'pg' fields as well, but
我的“op”字段得到了一个新类型。现在我也想检索我的“pg”字段,但是
select op, pg).SingleOrDefault();
doesn't work.
不起作用。
How can I select everything from both tables so that they appear in my new pageObject type?
如何从两个表中选择所有内容,以便它们出现在我的新 pageObject 类型中?
采纳答案by ljs
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();
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.:-
这将使 pageObject 成为匿名类型的 IEnumerable,因此 AFAIK 您将无法将其传递给其他方法,但是,如果您只是获取要在当前使用的方法中使用的数据,则完全没问题。您还可以在匿名类型中命名属性,即:-
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();
This will enable you to say:-
这将使您能够说:-
if (pageObject.PermissionName.FooBar == "golden goose") Application.Exit();
For example :-)
例如 :-)
回答by Konrad Rudolph
回答by aku
change
改变
select op)
to
到
select new { op, pg })
回答by Codewerks
If you don't want to use anonymous types b/c let's say you're passing the object to another method, you can use the LoadWith load option to load associated data. It requires that your tables are associated either through foreign keys or in your Linq-to-SQL dbml model.
如果您不想使用匿名类型 b/c 假设您将对象传递给另一个方法,则可以使用 LoadWith 加载选项来加载关联数据。它要求您的表通过外键或在您的 Linq-to-SQL dbml 模型中关联。
db.DeferredLoadingEnabled = false;
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<ObjectPermissions>(op => op.Pages)
db.LoadOptions = dlo;
var pageObject = from op in db.ObjectPermissions
select op;
// no join needed
Then you can call
然后你可以打电话
pageObject.Pages.PageID
Depending on what your data looks like, you'd probably want to do this the other way around,
根据您的数据的样子,您可能想反过来这样做,
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Pages>(p => p.ObjectPermissions)
db.LoadOptions = dlo;
var pageObject = from p in db.Pages
select p;
// no join needed
var objectPermissionName = pageObject.ObjectPermissions.ObjectPermissionName;
回答by daviddeath
If the anonymous type causes trouble for you, you can create a simple data class:
如果匿名类型给你带来麻烦,你可以创建一个简单的数据类:
public class PermissionsAndPages
{
public ObjectPermissions Permissions {get;set}
public Pages Pages {get;set}
}
and then in your query:
然后在您的查询中:
select new PermissionsAndPages { Permissions = op, Page = pg };
Then you can pass this around:
然后你可以传递这个:
return queryResult.SingleOrDefault(); // as PermissionsAndPages