当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - LINQ with Conditional Left Join

问题描述:

Hi there i am a newbie in linq,

i tried to do a left join query using LinQ. This is my query:

SELECT B.[ID]

,A.[GroupID]

,B.[Year]

,B.[Month]

,B.[Col1]

,B.[Col2]

,B.[Col3]

,B.[Col4]

,B.[Col5] FROM MsGroups A

LEFT JOIN MsCKPNS B on

A.GroupId = B.GroupId and B.Year = 2014 and B.Month = 3

Here is my linq query:

Dim msckpn = (From g In db.MsGroup

Group Join c In db.MsCKPNs

On g.GroupId Equals c.GroupID Into

Group From a In Group.DefaultIfEmpty() _

Where a.Month = Month And a.Year = Year

Select New MsCKPN With {

.ID = Nothing,

.GroupID = g.GroupId,

.Month = Month,

.Year = Year,

.Col1 = a.Col1,

.Col2 = a.Col2,

.Col3 = a.Col3,

.Col4 = a.Col4,

.Col5 = a.Col5

}).ToList

But, it shows error : The entity or complex type 'MvcMISD.MsCKPN' cannot be constructed in a LINQ to Entities query.

What should i do to fix the query?

Thanks

网友答案:

You can't create an entity like this. The reason is that you are not supposed to be able to create an entity which isn't linked to a record or records in the database. An explanation of this is given in the comments below this answer: http://stackoverflow.com/a/5325861/1737957 You can create an anonymous type which has the fields you want.

Some other parts of your code look a little strange but I'm not so familiar with LINQ in VB so I could be misunderstanding your query.

分享给朋友:
您可能感兴趣的文章:
随机阅读: