[英]MVC 5 select from 2 model linq expression
For example i have 2 model. 例如我有2模型。
ASpnetUserRoles and ASPnetRoles ASpnetUserRoles和ASPnetRoles
i want to select ASPnetRoles.Name,ASPnetROles.ID where ASPnetRoles.ID in ASPnetUserRoles. 我想选择ASPnetRoles.Name,ASPnetROles.ID,其中ASPnetUserRoles中的ASPnetRoles.ID。
i only know how to write in SQL 我只知道如何用SQL编写
select * from modalA where modelA.id in(select modelB.id from modelB)
if you enable " Lazy Loading " , you can use this linq query : 如果启用“ 延迟加载 ”,则可以使用以下linq查询:
using(var db = ..."context"..)
{
var q = db.AspNetUserRoles.where(c=>c.UserID = userIdVal)
.select(z=> new { RoleId = z.RoleId
,userId = z.UserId
,RoleName = z.AspNetRole.RoleName
})
.toList();
}
This is what i did eventually. 这就是我最终所做的。
IEnumerable<IdentityRole> ro;
ro = (from p in haha join ur in aspNetUser.AspNetRoles on p.Id equals ur.Id select p);
Here a generic sample you must create a class (no EF), and store the result to it. 在这里,您必须创建一个通用样本(没有EF),并将结果存储到其中。
IQueryable<ResultClass> result=from t1 in db.Table1
join t2 in db.Table2
//Here the relation fields
on t1.IdTable1 equals t2.IdTable2
//Here where conditios and/or orderby
select new ResultClass()
{
Field1=t1.SomeField,
Field2=t2.SomeField,
//all need fields
}
Use the result 使用结果
result.ToList()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.