1

I have a problem about list to list problem.

My Models:

public partial class OrderDetail
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public string SparePartName { get; set; }
    public Nullable<decimal> SparePartPrice { get; set; }
    public Nullable<decimal> Labor { get; set; }
    public Nullable<decimal> Total { get; set; }
    public string Comment { get; set; }
}

public partial class Orders
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public int StorageId { get; set; }
    public int UserId { get; set; }
    public Nullable<System.DateTime> DateEntry { get; set; }
    public Nullable<System.DateTime> DateRelease { get; set; }
    public Nullable<System.DateTime> DateEstimatedRelease { get; set; }
    public Nullable<System.DateTime> DateUpdate { get; set; }
    public Nullable<decimal> Price { get; set; }
    public int Status { get; set; }
    public string FollowCode { get; set; }
    public string DeviceBrand { get; set; }
    public string DeviceModel { get; set; }
    public string DeviceSerialNumber { get; set; }
    public string DeviceComplaint { get; set; }
    public string Comment { get; set; }
}

My ViewModel:

public class VMOrderEdit
{
    public int Id { get; set; }
    public int StorageId { get; set; }
    public int UserId { get; set; }
    public string StorageName { get; set; }
    public string CustomerName { get; set; }
    public string CustomerTelephone { get; set; }
    public DateTime? DateEntry { get; set; }
    public DateTime? DateUpdate { get; set; }
    public DateTime? DateRelease { get; set; }
    public DateTime? DateEstimatedRelease { get; set; }
    public decimal? Price { get; set; }
    public string StatusName { get; set; }
    public string FollowCode { get; set; }
    public string DeviceBrand { get; set; }
    public string DeviceModel { get; set; }
    public string DeviceSerialNumber { get; set; }
    public string DeviceComplaint { get; set; }
    public string Comment { get; set; }
    public int MyProperty { get; set; }
    public List<VMOrderEditDetails> Details { get; set; }
}
public class VMOrderEditDetails
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public string SparePartName { get; set; }
    public decimal SparePartPrice { get; set; }
    public decimal Labor { get; set; }
    public decimal Total { get; set; }
    public string Comment { get; set; }
}

My Controller:

List<VMOrderEdit> OrderEditObj = (from o in DB.Orders
                                  join sn in DB.OrderStatusName on o.Status equals sn.Id
                                  join st in DB.Storages on o.StorageId equals st.Id
                                  join c in DB.Customers on o.CustomerId equals c.Id
                                  where o.Id == Id
                                  select new VMOrderEdit()
                                  {
                                      Id = o.Id,
                                      StorageId = o.StorageId,
                                      StorageName = st.Name,
                                      UserId = o.UserId,
                                      CustomerName = c.NameSurname,
                                      CustomerTelephone = c.Telephone,
                                      DateEntry = o.DateEntry,
                                      DateUpdate = o.DateUpdate,
                                      DateRelease = o.DateRelease,
                                      Price = o.Price,
                                      StatusName = sn.Name,
                                      FollowCode = o.FollowCode,
                                      DeviceBrand = o.DeviceBrand,
                                      DeviceModel = o.DeviceModel,
                                      DeviceSerialNumber = o.DeviceSerialNumber,
                                      DeviceComplaint = o.DeviceComplaint,
                                      Comment = o.Comment,
                                      Details = DB.OrderDetail.Where(x => x.OrderId == o.Id).ToList()
                                      }
                               ).ToList();

on this line:

Details = DB.OrderDetail.Where(x => x.OrderId == o.Id).ToList()

visual studio gives this error:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List

how can i handle this error ?

2
  • Details is a VMOrderEditDetails list while DB.OrderDetail is (i'm assuming) an OrderDetail enumerable. Commented Oct 13, 2016 at 10:24
  • The question is, what 'OrderDetails' do you want to store in 'Details', then do a nested select to populate the 'Details' property, then ToList() Commented Oct 13, 2016 at 10:29

5 Answers 5

2

Your'e trying to assign a list of OrderDetail to VMOrderEditDetails which is bound to fail.

Your LINQ query should be something as follows. I haven't done the nullable checks and conversions on the Select. It can be done by you and is pretty straightforward. Please mark as answer if it helps.

 Details = DB.OrderDetail.Where(x => x.OrderId == o.Id).Select(x=>new VMOrderEditDetails{
    Id = x.Id,
    OrderId = x.OrderId,
    SparePartName = x.SparePartName,
    SparePartPrice = x.SparePartPrice,
    Labor = x.Labor,
    Total = x.Total,
    Comment = x.Comment
}).ToList()
Sign up to request clarification or add additional context in comments.

1 Comment

Thnak you for your effort and time.
2

The DB returns an object of OrderDetails but the items in the Details list property is of type VMOrderEditDetails..

You must convert each item from OrderDetails to VMOrderEditDetails before assigning.

  1. You can add a .Select after the .Where to convert to that type just before the .ToList() in the row with the exception (see in code of option 2).

    Details = DB.OrderDetail.Where(x => x.OrderId == o.Id)
                            .Select(x => new VMOrderEditDetails { /* your conversion*/})
                            .ToList()
    
  2. A better solution will be to add an extra GroupJoin before that and then to just have the conversion in the .Select and not also the retrieving of the data:

    List<VMOrderEdit> OrderEditObj = (from o in DB.Orders
                                      join sn in DB.OrderStatusName on o.Status equals sn.Id
                                      join st in DB.Storages on o.StorageId equals st.Id
                                      join c in DB.Customers on o.CustomerId equals c.Id
                                      join d in DB.OrderDetails on o.Id equals d.OrderId into d 
                                      where o.Id == Id
                                      select new VMOrderEdit()
                                      {
                                          Id = o.Id,
                                          StorageId = o.StorageId,
                                          //All the other properties
                                          Comment = o.Comment,
                                          Details = d.Select(x => new VMOrderEditDetails{ /*your convertion*/ }).ToList()
                                      }).ToList();
    
  3. And because you are using Entity Framework an even better option is to replace all the joins with the use of Navigation Properties, and then still to have the conversion at the bottom.

3 Comments

Thank you for efort and time.
@ysrtymz - you are welcome :) Did it help you understand the problem?
yes clearly. i was try to use a list object that didnt recognized/called/created.. right?
1

Try like this,

Details = DB.OrderDetail.Where(x => x.OrderId == o.Id)
            .Select(x => new VMOrderEditDetails {
                       Id = x.Id,
                       OrderId = x.OrderId,
                       SparePartName = x.SparePartName,
                       SparePartPrice = x.SparePartPrice.Value,
                       Labor = x.Labor.Value,
                       Total = x.Total.Value,
                       Comment = x.Comment
                   }).ToList()

3 Comments

Thnak you for your effort and time.
@ysrtymz, x.Labor is nullable type and requires x.Labor.Value but answer marked as accepted above for without .value?
i figured it out. thank you for warning. i updated my ViewModel with "decimal?"
1
DB.OrderDetail
  .Where(x => x.OrderId == o.Id)
  .ToList()

should be

DB.OrderDetail
  .Where(x => x.OrderId == o.Id)
  .Select(x => new VMOrderEditDetails () 
  { 
      Id = x.OrderId
  })
  .ToList<VMOrderEditDetails>()

as the types are different

Comments

0

Details is a List<VMOrderEditDetails> type but you are trying to assign it a List<OrderDetail> value so make it like this

Details = DB.VMOrderEditDetails.Where(x => x.OrderId == o.Id).ToList()

1 Comment

DB.VMOrderEditDetails certainly doesn't exist, as VMOrderEditDetails is a ViewModel class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.