C#转换列表在linq查询中抛出异常

标签 c# asp.net-mvc entity-framework linq

我有一个关于列表到列表的问题。

我的模型:

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; }
}

我的 View 模型:

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; }
}

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

在这一行:

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

visual studio 给出了这个错误:

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

我该如何处理这个错误?

最佳答案

您尝试将 OrderDetail 列表分配给 VMOrderEditDetails,这注定会失败。

您的 LINQ 查询应如下所示。我还没有对 Select 进行可空检查和转换。它可以由您完成并且非常简单。如果有帮助,请标记为答案。

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

关于C#转换列表在linq查询中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018314/

相关文章:

c# - 格式化字符串电话号码 MVC Razor 而不解析为十进制

c# - MVC3 自定义验证

C#的编译器设计——前向引用

asp.net-mvc - 如何在不模拟的情况下对使用 UpdateModel 的操作进行单元测试?

asp.net-mvc - TextBoxFor 中 "htmlAttributes"的类型以及如何向其添加更多属性

c# - EntityFramework 级联更新

c# - EF 4.1 代码优先 : Many to Many

c# - Entity Framework 保存循环中的第一项,但不保存其他项

c# - 如何通过 DataReader/DataTable 来查看?

c# - 使命名空间忽略某些文件夹 C# visual studio