Linq to Entities over EF 4 on SQL Server 2005

'Unable to compare elements of type' System.Collections.Generic.ICollection`1 '. Only primitive types (such as Int32, String, and Guid) and entity types are supported. ''

Entities and relationships (navigation properties):

Case (1 <-> ∞) MeetingCase (1 <-> ∞) MeetingCaseOutcomes 

Violation Code:

 IQueryable<Case> cases; // this is obviously attached to a context and able to access data) var broken = cases .Where(c => c.MeetingCases .Where(mc => mc.ExpectedStartDateTime <= DateTime.Now) .Any(m => m.MeetingCaseOutcomes == null || m.MeetingCaseOutcomes.Count == 0)); 

I assume that the problem is due to the lack of support for the "Any" operator, although I thought this would work in EF 4 (as support for the associated "Contains" operator was added).

How do I rebuild this call to create what I want?

I have been working with EF for several months now and understand many of the running hours.

UPDATE:

The Where clause contains the predicate above:

 c.MeetingCases.Where(mc => mc.ExpectedStartDateTime <= DateTime.Now) .Any(m => m.MeetingCaseOutcomes == null || m.MeetingCaseOutcomes.Count == 0) 

Because Any returns a boolean, the whole thing produces a predicate expression.

Furthermore, the purpose of this logic is to return a set of Case objects that do not have MeetingCaseOutcome entries for any MeetingCase entries where the meeting has already taken place (hence, a comparison with DateTime.Now). This is part of the meeting planning system, and this should verify that the results from each meeting are entered into the system after the meeting.

+4
source share
2 answers

EF 1 and EF 4 support .Any() . The problem is that you are not using it enough. :)

 IQueryable<Case> cases; // this is obviously attached to a context and able to access data) var broken = cases .Where(c => c.MeetingCases .Where(mc => mc.ExpectedStartDateTime <= DateTime.Now) .Any(m => !m.MeetingCaseOutcomes.Any())); 
+2
source

This part is the problem:

 Where(c => c.MeetingCases .Where.. 

.Where requires Expression<Func<T,bool>> , in other words - a predicate that returns true / false. But you provide it with a predicate that returns a sequence of elements.

I think the second .Where needs to be changed to .Any or .All .

I am trying to decrypt the request that you are trying to execute, but I assume that you need a Case list where at least one associated MeetingCase has an ExpectedStartDateTime until today.

So your request should be:

 var cases = cases .Where(case => case.MeetingCases .Any(caseMeeting => caseMeeting.ExpectedStartDateTime <= DateTime.Now)); 

I'm not sure what you are trying to do with this last .Any sentence.

Also - do not forget about problems with loading / projection. You say "it is context bound", which is good, but c.MeetingCases will not return anything if you do not have lazy loading or furious loading in advance.

+1
source

Source: https://habr.com/ru/post/1340742/


All Articles