ysrtymz
ysrtymz

Reputation: 63

C# Converting List throwing exception in linq query

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 ?

Upvotes: 1

Views: 660

Answers (5)

jl_
jl_

Reputation: 5539

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

Upvotes: 1

Gilad Green
Gilad Green

Reputation: 37299

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.

Upvotes: 2

Ritwik Sen
Ritwik Sen

Reputation: 296

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

Upvotes: 2

Aruna
Aruna

Reputation: 448

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

Upvotes: 1

Mostafiz
Mostafiz

Reputation: 7352

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

Upvotes: 0

Related Questions