Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Please can you help, I'm getting this error when the linq query is executed. Many thanks,


An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: Unable to create a constant value of type 'Programme'. Only primitive types or enumeration types are supported in this context.


Dim _ctx As New rsEntities
       Dim _dctx As New psEntities

       Dim users = (From u In _dctx.Users
                   Select u).ToArray()

       Dim userRoleIn = (From uri In _dctx.UserRoles
                        Select uri).ToArray()

       Dim progs = (From p In _dctx.Programmes
                   Select p).ToArray()

       Dim query = (From swt In _ctx.Workflows
                   Join wts In _ctx.WorkflowStatuses On wts.WTS_ID Equals swt.WTS_ID
                   Join sw In _ctx.StaffWorks On sw.SW_ID Equals swt.SW_ID
                   Join cr In _ctx.Requests On cr.CR_ID Equals swt.CR_ID
                   Join sp In _ctx.Progs On sp.SP_Id Equals sw.SP_ID
                   Join sps In _ctx.ProgStatuses On sps.SPS_ID Equals sp.SPS_ID
                   Join s In _ctx.Staffs On s.S_ID Equals sp.S_ID
                   Join p In progs On p.P_Id Equals sp.P_Id
                   Join u In users On u.S_ID Equals s.S_ID
                   Join uri In userRoleIn On uri.U_ID Equals u.U_Id
                   Where uri.P_Id.HasValue.Equals(15)
                   Where uri.Role_Id = 13
                   Where Not _ctx.Responses.Any(Function(es) es.CR_ID = cr.CR_ID And es.S_ID = s.S_ID)
                   Where sps.IsActive = True And wts.Description.ToLower.Contains("no")
                   Order By s.Surname, s.FirstName, cr.Title
                   Select New With {.ProgName = p.Name,
                                       .Surname = s.Surname,
                                       .FirstName = s.FirstName,
                                       .Number = s.StaffNumber,
                                       .Title = cr.Title,
                                       .Date = sw.StartDate,
                                       .Complete = wts.Description,
                                       .IsActive = If(sps.IsActive = True, "Yes", "No"),
                                       .HasAccess = If(uri.Role_Id = 23, "Yes", "No"),
                                       .HasEntry = If(String.IsNullOrEmpty(u.U_Id) = True, "No", "Yes"),
                                       .Email = u.U_Email}).ToList
Posted
Comments
Maciej Los 29-May-15 5:33am    
What line causes an error? I guess it's: Dim progs = ....
spankyleo123 29-May-15 5:37am    
It doesnt say which line ...but the whole query is highlighted in green with a dialog box giving the details of the error.

error in Dim query = (From swt In _ctx.Workflows

Because you're calling .ToArray() on the queries for users, userRoleIn and progs, they become in-memory collections of non-primitive types which can't be joined with a database-query (query).

Your options:

1) Remove the .ToArray()-calls.
In case you then get an error saying "The specified LINQ expression contains references to queries that are associated with different contexts" you would need to move the Entity-types Users, UserRoles, Programmes and Workflows into the same DbContext.

2) Leave these entities/joins out of the query "query" and perform this join client-side on the in-memory query results. Which could potentially have a huge performance drawback.

edit: Implementing option 2 would look something like this:
VB
' leaving out anything that's related to progs, users, userRoleIn
' and adding fields that are required for the following in-memory query
' (sp.P_Id, s.S_ID, u.U_Id)

        Dim query = (From swt In _ctx.Workflows
                    Join wts In _ctx.WorkflowStatuses On wts.WTS_ID Equals swt.WTS_ID
                    Join sw In _ctx.StaffWorks On sw.SW_ID Equals swt.SW_ID
                    Join cr In _ctx.Requests On cr.CR_ID Equals swt.CR_ID
                    Join sp In _ctx.Progs On sp.SP_Id Equals sw.SP_ID
                    Join sps In _ctx.ProgStatuses On sps.SPS_ID Equals sp.SPS_ID
                    Join s In _ctx.Staffs On s.S_ID Equals sp.S_ID
                    Where Not _ctx.Responses.Any(Function(es) es.CR_ID = cr.CR_ID And es.S_ID = s.S_ID)
                    Where sps.IsActive = True And wts.Description.ToLower.Contains("no")
                    Order By s.Surname, s.FirstName, cr.Title
                    Select New With {   .Surname = s.Surname,
                                        .FirstName = s.FirstName,
                                        .Number = s.StaffNumber,
                                        .Title = cr.Title,
                                        .Date = sw.StartDate,
                                        .Complete = wts.Description,
                                        .IsActive = If(sps.IsActive = True, "Yes", "No"),
                                        .P_Id = sp.P_Id,
                                        .S_ID = s.S_ID,
                                        .U_Id = u.U_Id,
                                    }).ToList

' and then performing the left-out joins/conditions with an in-memory query
' and re-assembling and completing the result-type:

Dim query2 = From q in query
             Join p In progs On p.P_Id Equals q.P_Id
             Join u In users On u.S_ID Equals q.S_ID
             Join uri In userRoleIn On uri.U_ID Equals q.U_Id
             Where uri.P_Id.HasValue.Equals(15)
             Where uri.Role_Id = 13
             Select New With {.ProgName = p.Name,
                              .Surname = q.Surname,
                              .FirstName = q.FirstName,
                              .Number = q.Number,
                              .Title = q.Title,
                              .Date = q.Date,
                              .Complete = q.Complete,
                              .IsActive = q.IsActive,
                              .HasAccess = If(uri.Role_Id = 23, "Yes", "No"),
                              .HasEntry = If(String.IsNullOrEmpty(u.U_Id) = True, "No", "Yes"),
                              .Email = u.U_Email}).ToList

(I tried to write it so it will actually work but since I can't test it, please don't rely on it - in any case, I think you'll understand how it would work in general.)
 
Share this answer
 
v3
Comments
F-ES Sitecore 29-May-15 8:35am    
Might also have a problem with the "Where Not _ctx.Responses.Any" code.

Basically, OP, your query statement can only contain things that can be directly converted to SQL. Linq to Objects is far far more flexible and powerful than SQL so it's easy to write something using Linq that can't be directly converted into SQL. For example the "WHERE NOT" above is a .net function...how can you convert a .net function to a SQL WHERE clause?
Sascha Lefèvre 29-May-15 8:42am    
True in general but I think the "where not" shouldn't be a problem - should be translated to "where not exists (...)".
F-ES Sitecore 29-May-15 9:03am    
Not the fact that is is where not, the fact that the condition for the where not is a .net function.
Maciej Los 29-May-15 9:06am    
Good advice in general (+5!), but you're wrong with NOT IN cluase. Please, see my answer.
Sascha Lefèvre 29-May-15 10:15am    
Thank you, Maciej!
I'm not convinced I'm wrong there ;)
As per small research, i think the problem is here:
C#
Where Not _ctx.Responses.Any(Function(es) es.CR_ID = cr.CR_ID And es.S_ID = s.S_ID)

Seems you're trying to use NOT IN(...) clause. It's not appreciated within Linq query.

Have a look here: Entity Framework - “Unable to create a constant value of type 'Closure type'…” error[^] for more details.

I'd suggest to use Except method[^] insted of NOT IN(...).
 
Share this answer
 
Comments
spankyleo123 29-May-15 9:20am    
Hi ML, as per my understanding the issue lies in these lines
Join p In progs On p.P_Id Equals sp.P_Id
Join u In users On u.S_ID Equals s.S_ID
Join uri In userRoleIn On uri.U_ID Equals u.U_Id
Where uri.P_Id.HasValue.Equals(15)
Where uri.Role_Id = 13


When i remove any references to users, userRoles and programmes I get no errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900