Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ to实体无法识别方法';单ToSingle(System.String)和#x27;方法,而此方法无法转换为存储表达式_C#_Asp.net Mvc_Linq - Fatal编程技术网

C# LINQ to实体无法识别方法';单ToSingle(System.String)和#x27;方法,而此方法无法转换为存储表达式

C# LINQ to实体无法识别方法';单ToSingle(System.String)和#x27;方法,而此方法无法转换为存储表达式,c#,asp.net-mvc,linq,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

我有以下代码:

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),导致上述错误。此处Amount在模型购物车中定义为
public float Amount{get;set;}

您可能还会发现,您不能在LinQ中使用toSingle,这与您不能使用toString方法的方式类似,因为没有SQL等价物:
您可能还会发现,您不能在LinQ中使用toSingle,这与您不能使用toString方法的方式类似,因为没有SQL等效:

该行导致错误,因为Linq正在尝试将其转换为SQL表达式,并且不存在对“Float.Parse()”的转换

我推荐的一种解决方法是使用数据传输对象(DTO)。只需创建另一个类似于Cart的类,称为CartDTO,但将“Amount”设置为字符串(或“u.Value”的任何类型)。然后,您可以手动或使用工具(如)将cardto映射到Cart

编辑:

从技术上讲,你甚至不需要DTO。类似的方法应该可以工作(注意select语句只剩下“select new”):

执行查询后:

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
            });
        }
List newLstCart=newlist();
foreach(lstcart中的旧变量)
{
添加(新购物车()
{
ID=old.ID,
Path=old.Path,
ProductCategory=old.ProductCategory,
数量=旧的。数量,
Title=旧的。Title,
Amount=float.Parse(old.Amount),
订单号=旧订单号,
prod\u Id=old.prod\u Id,
预览=旧的。预览
});
}

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

该行导致错误,因为Linq正在尝试将其转换为SQL表达式,并且不存在对“Float.Parse()”的转换

我推荐的一种解决方法是使用数据传输对象(DTO)。只需创建另一个类似于Cart的类,称为CartDTO,但将“Amount”设置为字符串(或“u.Value”的任何类型)。然后,您可以手动或使用工具(如)将cardto映射到Cart

编辑:

从技术上讲,你甚至不需要DTO。类似的方法应该可以工作(注意select语句只剩下“select new”):

执行查询后:

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
            });
        }
List newLstCart=newlist();
foreach(lstcart中的旧变量)
{
添加(新购物车()
{
ID=old.ID,
Path=old.Path,
ProductCategory=old.ProductCategory,
数量=旧的。数量,
Title=旧的。Title,
Amount=float.Parse(old.Amount),
订单号=旧订单号,
prod\u Id=old.prod\u Id,
预览=旧的。预览
});
}

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

您可以在内存中完成转换过程,您可以从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();

您可以在内存中完成转换过程,您可以从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();

您是否尝试过
Convert.ToDouble(u.Value)
?是否尝试过
Convert.ToDouble(u.Value)
?而不是Convert.tofloat()使用直接转换(float)(u.Value)而不是直接投票,是否有可能得到一些反馈,解释为什么答案不合适?这是不正确的。SQL(和EF)不是这样工作的——不能在一行中返回多个u结果。选择适用于每一行,每一行只有一个u.值。谢谢,重新阅读,我的大脑有一刻,删除了不好的建议,而不仅仅是否决票,是否有可能获得一些反馈,说明为什么答案不合适?这是不正确的。SQL(和EF)不是这样工作的——不能在一行中返回多个u结果。选择适用于每一行,每一行只有一个u值。谢谢,重新阅读,我的大脑有一刻,删除了错误的建议