选择并加入方法

时间:2019-02-28 16:00:08

标签: c# linq

我正在尝试使用join方法将多个表连接在一起并选择一些变量,但是它向我显示n.DateOfShipping和n.ProductName,这两个在当前上下文中不存在。我收到错误CS1061。

    var newestOrder = orderProducts.Where(op => op.DateOfShipping >= Today).
                    Join(products, op => op.ProductId, pr => pr.Id, (op, pr) => new { pr.ProductName, op.DateOfShipping, op.OrderId }).
                    Join(orders, op => op.OrderId, o => o.Id, (op, o) => new { op.DateOfShipping, op.OrderId, o.ClientId }).
                    Join(clients, o => o.ClientId, cl => cl.Id, (o, cl) => new { cl.ClientName});
    foreach (var n in newestOrder){
       Console.WriteLine("Date of shipping: {0}, Product Name: {1}, Client Name: {2}", n.DateOfShipping, n.ProductName, n.ClientName);
    }

这就是查询表单中的样子。

 var newestOrders = from op in orderProducts
                                join pr in products on op.ProductId equals pr.Id
                                join o in orders on op.OrderId equals o.Id
                                join c in clients on o.ClientId equals c.Id

                                where op.DateOfShipping > today
                                orderby op.DateOfShipping
                                select new
                                {
                                    op.DateOfShipping,
                                    pr.ProductName,
                                    c.ClientName
                                };

             foreach (var n in newestOrders)
             {
                 Console.WriteLine("Date of shipping: {0}, Product Name: {1}, Client Name: {2}", n.DateOfShipping, n.ProductName,n.ClientName);
             }

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为您忘记进行所需的工作了:

 var newestOrder = orderProducts.Where(op => op.DateOfShipping >= Today)
                   .Join(products, op => op.ProductId, pr => pr.Id, (op, pr) => new { pr.ProductName, op.DateOfShipping, op.OrderId })
                   .Join(orders, op => op.OrderId, o => o.Id, (op, o) => new { op.ProductName, op.DateOfShipping, o.ClientId }).
                    Join(clients, o => o.ClientId, cl => cl.Id, (o, cl) => new { o.ProductName, o.DateOfShipping, cl.ClientName});

    foreach (var n in newestOrder){
       Console.WriteLine("Date of shipping: {0}, Product Name: {1}, Client Name: {2}", n.DateOfShipping, n.ProductName, n.ClientName);
    }

每次连接后,您都必须在下一个连接中选择所需的所有字段,并在最终提取中选择