为什么我不能使用此查询

时间:2014-07-14 13:43:30

标签: vb.net linq

Dim lstOrderRegels = (From oRR In dbO.orders
                 Join ordreg In dbO.orderregels On oRR.ID Equals ordreg.ordernummer
                 Group Join a In dbO.artikelen On ordreg.artikelID Equals a.ID Into resultArt = Group
                 From art In resultArt.DefaultIfEmpty(New artikelen With {.ID = 0, .subgroep = 0})
                 Group Join subgr In dbO.productsubgroep On art.subgroep Equals subgr.ID Into result = Group
                 From sgr In result.DefaultIfEmpty(New productsubgroep With {.ID = 0, .productgroep_ID = 0})
                 Group Join hgr In dbO.productgroep On sgr.productgroep_ID Equals hgr.ID Into result2 = Group
                 From hg In result2.DefaultIfEmpty(New productgroep With {.ID = 0})
                 Join ses In dbO.sessie On oRR.sessieID Equals ses.ID
                 Where oRR.datum = werkdatum And oRR.status >= 40
                 Order By hg.code, sgr.code).ToList

每当我运行这个LINQ时,我得到Entities cannot be constructed in a LINQ to Entities query。我怎么能解决这个问题,我无法在谷歌上找到任何类似的解决方案。感谢

2 个答案:

答案 0 :(得分:0)

您的类productgroepproductsubgroep是通过框架生成的,您不能将(使用New)投射到通过框架生成的类。您有两个选项可以使用匿名类型,也可以为数据传输创建不同的类,并将结果投影到该类。

答案 1 :(得分:0)

            Dim lstOrderRegels As New List(Of xRapportDataQuery)
            Dim lstOrders = (From oRR In dbO.orders
                             Where oRR.datum = werkdatum And oRR.status >= 40).ToList

            Dim demOrderregels As New List(Of orderregels)
            For Each OrderData In lstOrders
                Dim oregelId As Integer = DMInt(OrderData.ID)
                Dim oregel = (From ordr In dbO.orderregels Where ordr.ordernummer = oregelId).ToList

                If oregel IsNot Nothing Then
                    For Each item In oregel
                        demOrderregels.Add(item)
                    Next
                End If
            Next
            For Each regelData In demOrderregels
                Dim art As artikelen
                Dim sgr As productsubgroep
                Dim hg As productgroep

                Dim ordreg As orderregels = regelData
                Dim oRR As orders = (From ord In dbO.orders Where ord.ID = regelData.ordernummer).FirstOrDefault
                Dim ses As sessie = (From s In dbO.sessie Where s.ID = oRR.sessieID).FirstOrDefault
                If DMInt(regelData.artikelID) = 0 Then
                    art = New artikelen With {.ID = 0, .subgroep = 0}
                    sgr = New productsubgroep With {.ID = 0, .productgroep_ID = 0}
                    hg = New productgroep With {.ID = 0}
                Else
                    art = (From a In dbO.artikelen Where a.ID = regelData.artikelID).FirstOrDefault
                    sgr = (From sg In dbO.productsubgroep Where sg.ID = art.subgroep).FirstOrDefault
                    hg = (From pg In dbO.productgroep Where pg.ID = sgr.productgroep_ID).FirstOrDefault
                End If


                lstOrderRegels.Add(New xRapportDataQuery With {.art = art, .hg = hg, .ordreg = ordreg, .oRR = oRR, .ses = ses, .sgr = sgr})
            Next

好的问题解决了,也许不是最好的方法,但它有效,我没有遇到任何问题。