在LINQPad4中将查询表达式转换为lambda

时间:2014-07-28 17:38:15

标签: linq linqpad

虽然编码我遇到了一个LINQ查询,我能够在查询语法中完成但不能在lamda语法中完成。虽然这在应用程序中运行良好,但我想学习我试图做的查询语法。

基本上,我有一个包含视图的数据库,CO_Leather_V和CO_LeatherSizeColor_V。我还有两个类,CuttingOrder和CuttingOrderDetail。 CuttingOrderDetail包含完全string,int和float属性。 CuttingOrder类包含2个字符串属性和CuttingOrderDetails列表。

public class CuttingOrder
{
    public string cuttingOrderNo { get; set; }
    public string reserveSalesOrderNo { get; set; }
    public List<CuttingOrderDetail> details { get; set; }
}   

public class CuttingOrderDetail
{
    public string cuttingOrderNo { get; set; }
    public string reserveSalesOrderNo { get; set; }
    public string itemCode { get; set; }
    public string material { get; set; }
    public string color { get; set; }
    public string size { get; set; }
    public int qty { get; set; }
    public float squareFeet { get; set; }
    public float squareFeetUsed { get; set; }
}

我用来获取具有给定SalesOrder的所有CuttingOrders列表的查询表达式是

cos = (from l in db.CO_Leather_Vs
    where l.orderNo == Globals.orderNo
    select new Globals.CuttingOrder
    {
        cuttingOrderNo = "NOT SET",
        reserveSalesOrderNo = "FAKE_SO_NO",
        details = (
            from d in db.CO_LeatherSizeColor_Vs
            select new Globals.CuttingOrderDetail
            {
                cuttingOrderNo = d.orderNo
            }
    ).ToList()
}).ToList();

我使用以下查询将其转换为在LINQPad中工作,但我无法在lambda窗格中显示任何内容。

void Main()
{

var p = (from l in CO_Leather_V
        select new CuttingOrder
        {
        cuttingOrderNo = "NOT SET",
        reserveSalesOrderNo = "FAKE_SO_NO",
        details = (
        from d in CO_LeatherSizeColor_V
        select new CuttingOrderDetail
        {
            cuttingOrderNo = d.OrderNo
        }
        ).ToList()
        }).ToList();


p.Dump();

}


// Define other methods and classes here
public class CuttingOrder
{
    public string cuttingOrderNo { get; set; }
    public string reserveSalesOrderNo { get; set; }
    public List<CuttingOrderDetail> details { get; set; }
}   

public class CuttingOrderDetail
{
    public string cuttingOrderNo { get; set; }
    public string reserveSalesOrderNo { get; set; }
    public string itemCode { get; set; }
    public string material { get; set; }
    public string color { get; set; }
    public string size { get; set; }
    public int qty { get; set; }
    public float squareFeet { get; set; }
    public float squareFeetUsed { get; set; }
}

如果有人知道如何以lambda形式执行linq查询或知道为什么LINQPad无法生成lamda表单,那将非常感激。

1 个答案:

答案 0 :(得分:0)

这应该有效:

var p = CO_Leather_V.Select(l=> new CuttingOrder
        {
            cuttingOrderNo = "NOT SET",
            reserveSalesOrderNo = "FAKE_SO_NO",
            details = CO_LeatherSizeColor_V.Select(d=>new CuttingOrderDetail  {cuttingOrderNo = d.OrderNo}).ToList()
        }).ToList();

但是,CO_LeatherSizeColor_V未引用l,因此您每次都会获得该表中的所有内容。您可能需要以下内容:

            details = l.LeatherSizeColor.Select(d=>new CuttingOrderDetail  {cuttingOrderNo = d.OrderNo}).ToList()

代替那条线。