A type that implements IEnumerable 'System.Collections.Generic.List`1' cannot be initialized in a LINQ to Entities query
Asked Answered
C

3

8

I am trying to create object of some class inside the Linq query but gives me an error as set title of the question.

My query is:

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();

I have define the list of class in as object inside 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; }
}
Canzone answered 15/11, 2016 at 10:38 Comment(0)
J
14

Initialise the secondClass List inside your ViewModel constructor:

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

Remember to remove the initialisation from the Linq query.

Edit

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();

Edit 2

Your oneViewModel should look something like this:

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

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

The linq query should look like this:

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();

Now that you will have a list of oneViewModel objects.

Juanitajuanne answered 15/11, 2016 at 10:42 Comment(6)
How can I do both in one? I have already declare the public second classlist inside the ABC.Canzone
Thanks for the response Ryan but I don't know how can I declare that and use to set it in constructor. I have approched Pawel's answer.Canzone
@padhiyar To be clear, would you like to create a new (empty) list per oneViewModel or would you like to create a list from the values in abc?Juanitajuanne
I want to create only object of list inside that oneViewModel class of list inside linq query because I ahead get dynamic data after that query which I will use to set SecondClassList data.Canzone
Sorry I'm still not sure that I understand. Are you going to be populating secondClass later on after this query is complete?Juanitajuanne
Let us continue this discussion in chat.Canzone
E
2

You need to execute query first and then initialize list, e.g.:

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();
Eddaeddana answered 15/11, 2016 at 10:48 Comment(1)
The problem with this is that is will execute the query against the database after that first .ToList() and do the rest in memory with Linq to Objects. This can cause more columns to be pulled across the wire than necessary (bad for performance), and could cause even worse issues if using navigation properties in your Select statement (extra queries with Lazy loading, or missing data without Lazy loading).Drench
D
0

You apparently can't create an empty list like that in Linq to Entities, but you can trick it by putting a dummy item into the list and then filtering it out via a Where clause like below.

The accepted solution wouldn't work for my case because I had a more complex query with oneViewModel being initialized twice in the query, once with real data and once with empty data. I was getting the error The type '{myType}' appears in two structurally incompatible initializations within a single LINQ to Entities query.. I needed to specify more columns inside the query instead of letting the constructor do it behind the scenes.

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();
Drench answered 13/9, 2023 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.