LINQ to Entities无法识别方法' Single ToSingle(System.String)'方法,并且此方法无法转换为商店表达式

时间:2016-03-11 09:03:06

标签: c# asp.net-mvc linq

我有以下代码:

lstcart = (from t in context.T_Order
                           join  s in context.T_OrderDetails on t.ID equals s.Order_ID
                           join  u in context.T_OrderDetailSpecification  on s.ID  equals u.OrderDetails_ID
                           join p in context.M_Product on s.Product_ID  equals p.ID
                           join qrs in context.M_ProductCategory on p.ProductCategory_ID equals qrs.ID
                           where t.User_ID == u_id && s.OrderStatus_ID == 1 || s.OrderStatus_ID == 17
                           select new Cart
                           {
                               ID = s.ID,
                               Path = p.VirtualPath,
                               ProductCategory = qrs.Title,
                               Quantity = 1,
                               Title = p.Title,
                               Amount = Convert.ToSingle(u.Value),
                               Order_Id= s.Order_ID,
                               prod_Id= p.ID,
                               Preview = s.IsPreviewRequired
                           }).ToList();

其中Amount = float.Parse(u.Value)行,导致上述错误.Here Amount在模型购物车中被定义为public float Amount { get; set; }

3 个答案:

答案 0 :(得分:2)

您可以在内存中完成转换过程,您可以从db返回匿名类型,而不是将其转换为您的对象。

var lstcart = (from t in context.T_Order
                   join s in context.T_OrderDetails on t.ID equals s.Order_ID
                   join u in context.T_OrderDetailSpecification on s.ID equals u.OrderDetails_ID
                   join p in context.M_Product on s.Product_ID equals p.ID
                   join qrs in context.M_ProductCategory on p.ProductCategory_ID equals qrs.ID
                   where t.User_ID == u_id && s.OrderStatus_ID == 1 || s.OrderStatus_ID == 17
                   select new /// anonymous type
                   {
                       ID = s.ID,
                       Path = p.VirtualPath,
                       ProductCategory = qrs.Title,
                       Quantity = 1,
                       Title = p.Title,
                       Amount = u.Value,
                       Order_Id = s.Order_ID,
                       prod_Id = p.ID,
                       Preview = s.IsPreviewRequired
                   }).AsEnumerable()
                            .Select(result => new Cart /// create your object after query execution in memory
                             {
                                ID = result.ID,
                                Path = result.VirtualPath,
                                ProductCategory = result.Title,
                                Quantity = result.Quantity,
                                Title = result.Title,
                                Amount = Convert.ToSingle(result.Amount),
                                Order_Id = result.Order_ID,
                                prod_Id = result.ID,
                                Preview = result.IsPreviewRequired
                            }).ToList();

答案 1 :(得分:0)

你可能还会发现你不能在LinQ中使用toSingle,类似于你不能使用toString方法的方式,因为没有SQL等效: Problem with converting int to string in Linq to entities

答案 2 :(得分:0)

由于Linq尝试将其转换为SQL表达式,因此该行导致错误,并且不存在' Float.Parse()'

的翻译。

我建议的一种解决方法是使用数据传输对象(DTO)。只需创建另一个类似购物车的类,称为 CartDTO ,但设置' 金额'作为一个字符串(或任何类型的' u.Value ')。然后,您可以手动或使用Automapper等工具将 CartDTO 映射到购物车

修改

从技术上讲,你甚至不需要DTO。这样的事情应该有效(注意选择陈述只是留下'选择新的' ):

lstcart = (from t in context.T_Order
                   join s in context.T_OrderDetails on t.ID equals s.Order_ID
                   join u in context.T_OrderDetailSpecification on s.ID equals u.OrderDetails_ID
                   join p in context.M_Product on s.Product_ID equals p.ID
                   join qrs in context.M_ProductCategory on p.ProductCategory_ID equals qrs.ID
                   where t.User_ID == u_id && s.OrderStatus_ID == 1 || s.OrderStatus_ID == 17
                   select new
                   {
                       ID = s.ID,
                       Path = p.VirtualPath,
                       ProductCategory = qrs.Title,
                       Quantity = 1,
                       Title = p.Title,
                       Amount = u.Value,
                       Order_Id = s.Order_ID,
                       prod_Id = p.ID,
                       Preview = s.IsPreviewRequired
                   }).ToList();

执行查询后:

List<Cart> newLstCart = new List<Cart>();
        foreach (var old in lstcart)
        {
            newLstCart.Add(new Cart()
            {
                ID = old.ID,
                Path = old.Path,
                ProductCategory = old.ProductCategory,
                Quantity = old.Quantity,
                Title = old.Title,
                Amount = float.Parse(old.Amount),
                Order_Id = old.Order_Id,
                prod_Id = old.prod_Id,
                Preview = old.Preview
            });
        }

但是,我仍然建议尝试使用DTO和Automapper,它们在更复杂的情况下非常有用。