1

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

mutokenji
  • 525
  • 1
  • 5
  • 14
  • possible duplicate of [The entity cannot be constructed in a LINQ to Entities query](http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query) – Rawling Jul 17 '14 at 10:09

1 Answers1

0

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: https://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.

Community
  • 1
  • 1
jwg
  • 5,547
  • 3
  • 43
  • 57