使用linq中的select new返回包含集合的类

时间:2013-12-09 19:15:21

标签: c# linq

所以我有这个DeploymentOrderInfo类:

public class DeploymentOrderInfo
{
    public string SOPNUMBE { get; set; }
    public string CUSTNMBR { get; set; }
    public string CNTCPRSN { get; set; }
    public string SHIPMTHD { get; set; }
    public string ShipToName { get; set; }
    public string ADDRESS1 { get; set; }
    public string ADDRESS2 { get; set; }
    public string ADDRESS3 { get; set; }
    public string CITY { get; set; }
    public string STATE { get; set; }
    public string ZIPCODE { get; set; }
    public IList<DeploymentOrderItems> DeploymentOrderItems { get; set; }
}

public class DeploymentOrderItems
{
    public int LNITMSEQ { get; set; }
    public int CMPNTSEQ { get; set; }
    public string ITEMNMBR { get; set; }
    public decimal TotalQuantity { get; set; }
    public string UOFM { get; set; }
    public bool ItemFulfilled { get; set; }
    public short ITEMTYPE { get; set; }
}

我想在linq查询中使用它。令我困惑的部分是我将如何填充类中的DeploymentOrderItems集合。以下是我到目前为止的情况:

var result = 
    (from hdr in context.SOP10100
    join det in context.SOP10200
    on new { hdr.SOPNUMBE, hdr.SOPTYPE } equals new { det.SOPNUMBE, det.SOPTYPE }
    join im in context.IV00101
    on det.ITEMNMBR equals im.ITEMNMBR
    where hdr.SOPNUMBE == orderNumber
    where det.CMPNTSEQ == 0
    orderby det.QUANTITY descending
    select new
    {
        SOPNUMBE = hdr.SOPNUMBE,
        CUSTNMBR = hdr.CUSTNMBR,
        CNTCPRSN = hdr.CNTCPRSN,
        SHIPMTHD = hdr.SHIPMTHD,
        ShipToName = hdr.ShipToName,
        ADDRESS1 = hdr.ADDRESS1,
        ADDRESS2 = hdr.ADDRESS2,
        ADDRESS3 = hdr.ADDRESS3,
        CITY = hdr.CITY,
        STATE = hdr.STATE,
        ZIPCODE = hdr.ZIPCODE,
        ITEMNMBR = det.ITEMNMBR,
        TotalQuantity = det.QUANTITY,
        UOFM = det.UOFM,
        ITEMTYPE = im.ITEMTYPE
    }).ToList();

DeploymentOrderInfo orderInfo = null;

if (result != null)
{
    orderInfo = new DeploymentOrderInfo();

    orderInfo.SOPNUMBE = result[0].SOPNUMBE;
    orderInfo.CUSTNMBR = result[0].CUSTNMBR;
    orderInfo.CNTCPRSN = result[0].CNTCPRSN;
    orderInfo.SHIPMTHD = result[0].SHIPMTHD;
    orderInfo.ShipToName = result[0].ShipToName;
    orderInfo.ADDRESS1 = result[0].ADDRESS1;
    orderInfo.ADDRESS2 = result[0].ADDRESS2;
    orderInfo.ADDRESS3 = result[0].ADDRESS3;
    orderInfo.CITY = result[0].CITY;
    orderInfo.STATE = result[0].STATE;
    orderInfo.ZIPCODE = result[0].ZIPCODE;

    foreach (var item in result)
    {
        orderInfo.DeploymentOrderItems.Add(new DeploymentOrderItems { ITEMNMBR = item.ITEMNMBR, TotalQuantity = item.TotalQuantity, UOFM = item.UOFM, ITEMTYPE = item.ITEMTYPE });
    }
}

return orderInfo;

但那段代码很愚蠢。我想在linq查询中填充整个类。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

请原谅我,我不打算复制你的所有代码,但基本的想法是

from hdr in context.SOP10100
....
select new DeploymentOrderInfo
{
    SOPNUMBE = hdr.SOPNUMBE,
    ....
    DeploymentOrderItems = (from det in context.SOP10200
                            where hdr.SOPNUMBE == det.SOPNUMBE 
                               && hdr.SOPTYPE == det.SOPTYPE
                             join im in context.IV00101
                             on det.ITEMNMBR equals im.ITEMNMBR
                             select new DeploymentOrderItems
                             {
                                 ITEMNMBR = item.ITEMNMBR,
                                 ...
                             })
}

我认为AutoMapper可能是一个有趣的工具。