c# - 在映射中使用 Linq 查询

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

我有一个类,其中包含三个运行良好的方法:

  1. 从表中获取数据:

        public IList<RetriveOperatorAcYo> GetRetriveOperatorListGrid(int Uid)
        {
            var persons = (from p in _Context.People
                          join a in _Context.Accounts on p.PersonId equals a.PersonId
                          join b in _Context.BusinessTypes on a.BusinessTypeId equals b.BusinessTypeId
                          join d in _Context.AccountUserRelations on a.AccountId equals d.AccountId
                          where b.Name == AccountBusinessTypes.Operator && a.IsDelete == false && d.Active == true && d.UserId == Uid
                          select p);
            return MapToRetriveOperatorYoList(persons);
        }
    
  2. 用作将列表分隔为单个元素的中间函数:

        private IList<RetriveOperatorAcYo> MapToRetriveOperatorYoList(IEnumerable<EntityFramework.Person> persons)
        {
            return persons.Select(MapToRetirveOperatorYo).ToList();
        }
    
  3. 作为生成结果的映射函数:

        private RetriveOperatorAcYo MapToRetirveOperatorYo(EntityFramework.Person person)
        {
            var vendorYo = new RetriveOperatorAcYo
            {
                Id = person.PersonId,
                FirstName = person.FirstName,
                LastName = person.LastName,
                MobileNumber = person.MobileNumber,
                LandlineNumber = person.LandlineNumber
            }
    
            return vendorYo;
       }
    

现在我想在第一个函数中仅获取特定列的数据。

我搜索了一些技术并尝试了这个:

var persons = (from p in _Context.People
               join a in _Context.Accounts on p.PersonId equals a.PersonId
               join b in _Context.BusinessTypes on a.BusinessTypeId equals b.BusinessTypeId
               join d in _Context.AccountUserRelations on a.AccountId equals d.AccountId
               where b.Name == AccountBusinessTypes.Operator && a.IsDelete == false && d.Active == true && d.UserId == Uid
               select new 
               {
                  PersonId = p.PersonId,
                  FirstName = p.FirstName,
                  LastName = p.LastName,
                  MobileNumber = p.MobileNumber,
                  LandlineNumber = p.LandlineNumber
               });

但它在第二个函数中给了我错误:

"The entity or complex type cannot be constructed in a LINQ to Entities query."

是否有任何解决方案如何映射数据并仅返回具有特定详细信息的特定映射类。

感谢您提前提供的帮助。

最佳答案

要使用 3 个方法维护模式并执行所需的部分查询,同时不在第一个方法中使用现有的 DTO,您将不得不诉诸于传递动态对象并更改方法签名。像这样的东西应该可以工作,尽管不推荐 - 请注意在第二个和第三个方法中使用dynamic:

public IList<RetriveOperatorAcYo> GetRetriveOperatorListGrid(int Uid)
{
    var persons = (from p in _Context.People
        join a in _Context.Accounts on p.PersonId equals a.PersonId
        join b in _Context.BusinessTypes on a.BusinessTypeId equals b.BusinessTypeId
        join d in _Context.AccountUserRelations on a.AccountId equals d.AccountId
        where b.Name == AccountBusinessTypes.Operator && a.IsDelete == false && d.Active == true && d.UserId == Uid
        select new 
        {
            PersonId = p.PersonId,
            FirstName = p.FirstName,
            LastName = p.LastName,
            MobileNumber = p.MobileNumber,
            LandlineNumber = p.LandlineNumber
         });
    return MapToRetirveOperatorYo(persons);
}

private IList<RetriveOperatorAcYo> MapToRetriveOperatorYoList(IEnumerable<dynamic> persons)
{
    return persons.Select(MapToRetirveOperatorYo).ToList();
}

private RetriveOperatorAcYo MapToRetirveOperatorYo(dynamic person)
{
    var vendorYo = new RetriveOperatorAcYo
    {
        Id = person.PersonId,
        FirstName = person.FirstName,
        LastName = person.LastName,
        MobileNumber = person.MobileNumber,
        LandlineNumber = person.LandlineNumber
    }
    return vendorYo;

}

更好的选择是将您的第一个方法更改为select new RetriveOperatorAcYo,如 this answer 中所述。 .

关于c# - 在映射中使用 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755972/

相关文章:

c# - .net 中的 HashTable/Dictionary 实现选择了哪种类型的冲突解决方案?

c# - WPF Listview 组 header 不显示 : Any Ideas?

c# - Linq SQL GroupJoin 错误

c# - 检查 Razor 中模型列表项的计数

c# - 可使用 IEnumerable 属性和列表成员序列化

c# - 计算数组的频率分布?

c# - 使用 LINQ 从 2 个集合中检索非重复项

c# - 从 DataTable 创建 XML

c# - 部分 View 验证错误在部分 View 上获取触发 View 获取调用本身

api - IE10 中的 Angular js 204 响应阻塞