开发者

Select hierarchical data via Linq2SQL

开发者 https://www.devze.com 2023-03-20 14:05 出处:网络
It\'s late here and I can\'t see the forest for the trees. I\'m trying to select hierarchical data with the following approach:

It's late here and I can't see the forest for the trees.

I'm trying to select hierarchical data with the following approach:

return
    from line in data
    group line by line.LieferscheinNr into g
    let kpf = g.First()
    select new DESSK1
    {
        K1LINK = MakeDfuePartnerID(kpf.Land, kpf.VerbandNr),
        K1LSNR = kpf.LieferscheinNr,
        K1LSDT = decimal.Parse(kpf.LieferscheinDatum.ToString("ddMMyyyy")),
        K1ILNW = kpf.IlnLieferanschrift,
        K1ILNH = kpf.IlnKunde,
        K1ILNL = kpf.IlnMandant,
        K1CPSD = 1,
        K1ILNB = kpf.IlnKunde,
        K1VSDT = decimal.Parse(kpf.LieferDatum.ToString("ddMMyyyy")),
        K1BSTN = kpf.BestellNr,

        // this is where the compiler complains
        DESSP2 = from pos in g
                 select new DESSP2
               开发者_如何学运维  {
                     P2LSNR = pos.LieferscheinNr,
                     P2POSN = pos.Pos,
                     P2EAN = pos.EAN,
                     P2LMG = pos.Menge,
                     P2BMG = pos.MengeSoll
                 }
    };

I think you can see what I'm trying here but it fails since DESSP2 is of type System.Data.Link.EntitySet<DESSP2> and the query is of type IEnumerable<DESSP2>.

There must be an easy way from IEnumerable<T> to EntitySet<T> but I can't see one.

Any ideas?


Ah, I knew this was an easy one. While thinking about how to populate the EntitySet within the query I found this perfectly working solution:

How do you cast an IEnumerable<t> or IQueryable<t> to an EntitySet<t>?

I wonder why the ToEntitySet() method is not part of the language. Looks to me like an usual problem if you want to fill hierachical linq tables.

The relevant part now looks like this:

    DESSP2 = (from pos in g
             select new DESSP2
             {
                 P2LSNR = pos.LieferscheinNr,
                 P2POSN = pos.Pos,
                 P2EAN = pos.EAN,
                 P2LMG = pos.Menge,
                 P2BMG = pos.MengeSoll
             }).ToEntitySet()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号