Dav*_*loe 6 c# linq asp.net-mvc entity-framework asp.net-mvc-4
这个项目让我发疯;-)我正在尝试做一个简单的查询加入两个表
我有以下内容:
存储库类方法
public IQueryable<ADPerson> FindAll(string UserId)
{
return (from p in db.ADPerson
select p);
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
var ADPersonList = from o in ADPersonDB.FindAll(GetUserId())
join c in MSDNTypeDB.FindAll(GetUserId()) on o.MsdnTypeId equals c.MsdnTypeId
select new ADPerson()
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription,
};
Run Code Online (Sandbox Code Playgroud)
我一直收到错误:
{"The specified LINQ expression contains references to queries that are associated with different contexts."}
Run Code Online (Sandbox Code Playgroud)
我还尝试添加到存储库类:
存储库类方法
public IQueryable<ADPerson> FindAll(string UserId)
{
//return (from p in db.ADPerson
// select p);
var query = from o in db.ADPerson
join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId
select new ADPerson()
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription,
};
return query;
}
Run Code Online (Sandbox Code Playgroud)
是否真的很难在两个表之间进行简单连接并在Entity框架中填充变量
谢谢
投射到匿名对象
var query = from o in db.ADPerson
join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId
select new
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription,
};
Run Code Online (Sandbox Code Playgroud)
然后映射回您的实体
foreach (var item in query)
{
var adPerson = new ADPerson
{
AdPersonId = item.AdPersonId,
SamAccountName = item.SamAccountName,
Description = item.Description,
DisplayName = item.DisplayName,
UserPrincipalName = item.UserPrincipalName,
Enabled = item.Enabled,
LastUpdated = item.LastUpdated,
OnlineAssetTag = item.OnlineAssetTag,
MsdnTypeId = item.MsdnTypeId,
MsdnSubscription = item.MsdnTypeDescription,
{
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15470 次 |
最近记录: |