c# - MVC 4. 和 Entity Framework 表连接

标签 c# asp.net-mvc linq entity-framework asp.net-mvc-4

这个项目让我抓狂 ;-) 我正在尝试做一个连接两个表的简单查询

我有以下内容:

存储库类方法

 public IQueryable<ADPerson> FindAll(string UserId)
    {
        return (from p in db.ADPerson
                select p); 


    }

在我的 Controller 中:

  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,


                    };

我一直收到错误:

{"The specified LINQ expression contains references to queries that are associated with different contexts."}

我还尝试添加到存储库类:

存储库类方法

 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;

    }

在两个表之间做一个简单的连接并填充 Entity Framework 中的变量真的很难吗

谢谢

最佳答案

投影到匿名对象

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,
  };

然后映射回你的实体

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,
  {
}

关于c# - MVC 4. 和 Entity Framework 表连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15416852/

相关文章:

javascript - 使用 css悬停时突出显示表格行

jquery - 使用 jquery 成功回调进行 asp .net 验证

c# - 对列表中的值进行分组并使用 C# 将输出转换为 JSON

c# - 将 unicode 字符串从 C# 发送到 Java

c# - 如何创建嵌套项目符号列表

c# - CellStyle 意外应用于工作表中的所有单元格 - NPOI?

asp.net - 如何处理 url 搜索词中的 web.config

c# - LINQ to Entities 自定义方法

c# - 在 LINQ 查询中将 KeyValuePair 转换为匿名类型

c# - 添加/求和两个数组