无法在LINQ to Entities查询中初始化实现IEnumerable 'System.Collections.Generic.List' 1'的类型

ma8fv8wu  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(112)

我试图在Linq查询中创建某个类的对象,但由于设置了问题的标题,因此给了我一个错误。
我的问题是:

List<oneViewModel> workOrderInfoList = (from abc in db.ABC
  join customer in db.Customers on abc.CustomerId equals customer.CustomerId into customers
  select new oneViewModel()
  {
     CustomerId = abc.CustomerId,
     OrderNumber = workOrderInfo.OrderNumber,
     OrderDate = abc.OrderDate,
     SecondClassList = new List<SecondClass>(),
  }).ToList();

我已经在oneViewModel中定义了类的列表作为对象。

public class ABC        
{
    public DateTime? WorkOrderDate { get; set; }
    public long CustomerId { get; set; }

    public string CustomerName { get; set; }

    public List<SecondClass> SecondClassList { get; set; }
}
s4n0splo

s4n0splo1#

ViewModel构造函数中初始化secondClass列表:

Public oneViewModel()
{
    SecondClassList = new List<SecondClass>();
)

记住从Linq查询中删除初始化。

编辑

List<oneViewModel> workOrderInfoList = (from abc in db.ABC
    join customer in db.Customers on abc.CustomerId equals customer.CustomerId into customers
    select new oneViewModel()
    {
        CustomerId = abc.CustomerId,
        OrderNumber = workOrderInfo.OrderNumber,
        OrderDate = abc.OrderDate,
        SecondClassList = abc.SecondClassList
    }).ToList();

编辑2

您的oneViewModel应该看起来像这样:

public class oneViewModel
{
    public oneViewModel
    {
        SecondClassList = new List<SecondClass>();
    }

    Public List<SecondClass> SecondClassList { get; set; }
}

linq查询应该如下所示:

List<oneViewModel> workOrderInfoList = (from abc in db.ABC
join customer in db.Customers on abc.CustomerId equals customer.CustomerId into customers
select new oneViewModel()
{
    CustomerId = abc.CustomerId,
    OrderNumber = workOrderInfo.OrderNumber,
    OrderDate = abc.OrderDate
}).ToList();

现在,您将拥有oneViewModel对象的列表。

wkyowqbh

wkyowqbh2#

你需要先执行query,然后初始化list,例如:

List<oneViewModel> workOrderInfoList = (from abc in db.ABC
  join customer in db.Customers on abc.CustomerId equals customer.CustomerId into customers).ToList()
  Select(n => new oneViewModel()
  {
     CustomerId = n.CustomerId,
     OrderNumber = workOrderInfo.OrderNumber,
     OrderDate = n.OrderDate,
     SecondClassList = new List<SecondClass>(),
  }).ToList();
pxq42qpu

pxq42qpu3#

显然,你不能像Linq to Entities那样创建一个空列表,但是你可以通过在列表中放入一个虚拟项,然后通过下面的Where子句过滤掉它来欺骗它。
可接受的解决方案对我的情况不起作用,因为我有一个更复杂的查询,其中oneViewModel在查询中被初始化了两次,一次是真实的数据,一次是空数据。错误:The type '{myType}' appears in two structurally incompatible initializations within a single LINQ to Entities query.我需要在查询中指定更多的列,而不是让构造函数在幕后完成。

List<oneViewModel> workOrderInfoList = (from abc in db.ABC
  join customer in db.Customers on abc.CustomerId equals customer.CustomerId into customers
  select new oneViewModel()
  {
     CustomerId = abc.CustomerId,
     OrderNumber = workOrderInfo.OrderNumber,
     OrderDate = abc.OrderDate,
     SecondClassList = new List<SecondClass>() { new SecondClass() }.Where(x => false).ToList(),
  }).ToList();

相关问题