繁体   English   中英

为什么我的linq to sql查询失败?

[英]Why does my linq to sql query fail?

我是.Net(和stackoverflow)的新手,所以遇到了一些问题。 一个令人困扰的问题是下面的这个linq查询。 有关代码的一些信息。 在我的项目中,CustomerCart是一个单独的Model类,其中product成员是产品列表。 如果我从选择的新CustomerCart部分中移除产品,则它运行良好并且有数据。 所以我认为这是我的语法。 我不能在分配构造函数中放置linq语句吗? 任何帮助将是感激的。 先感谢您。

var k = from s in store.Customers
        join p in store.ShoppingCarts on s.custId equals p.customerId
        select new CustomerCart()
        {
            FirstName = s.firstName,
            LastName = s.lastName,
            products = (from j in store.Products
                        where p.productId == j.productId
                        select j).ToList(),
            CartID = p.cartId,
            CustomerID = s.custId,
         };

**我收到编辑错误:传递到字典中的模型项的类型为'System.Data.Objects.ObjectQuery`1 [productShop.Models.CustomerCart]',但此字典要求模型项的类型为'productShop.Models。客户推车”。

很抱歉没有在我的问题中放置错误消息。

请尝试以下查询:

var k =
    from s in store.Customers
    join p in store.ShoppingCarts on s.custId equals p.customerId
    join j in store.Products on p.productId equals j.productId into products
    select new CustomerCart()
    {
        FirstName = s.firstName,
        LastName = s.lastName,
        products,
        CartID = p.cartId,
        CustomerID = s.custId,
     };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM