2

I have the following code...

examinerSpec.Predicate = examinerSpec.AndExpression(examinerSpec.Predicate,
     centreTemp =>centreTemp.ExaminerCentreAssociations.Any(
     examinerTemp =>examinerTemp.CentreRegionMaps.CentreNumber.
     Contains(**currentCentreNumber**) &&
     examinerTemp.CentreRegionMaps.Active == true &&
     examinerTemp.DateStart <= DateTime.Now &&
     (!examinerTemp.DateEnd.HasValue ||
     examinerTemp.DateEnd.Value >= DateTime.Now)));

CurrentCentreNumber in Contains(currentCentreNumber) is a string. I want to use an IEnumerable of centreNumbers. I tried the following...

examinerSpec.Predicate = examinerSpec.AndExpression(examinerSpec.Predicate,
   centreTemp =>
   centreTemp.ExaminerCentreAssociations.Any(
   examinerTemp =>
   examinerTemp.CentreRegionMaps.CentreNumber.
   Any(currentCentreNumber) &&
   examinerTemp.CentreRegionMaps.Active == true &&
   examinerTemp.DateStart <= DateTime.Now &&
   (!examinerTemp.DateEnd.HasValue ||
   examinerTemp.DateEnd.Value >= DateTime.Now)));

that doesn't work. The compiler tells me 'the type arguments for the method cannot be inferred'.

I really need the examinerTemp.CentreRegionMaps.CentreNumber to be filtered on an IEnumerable parameter.

Many thanks in advance for any help.

KF2
  • 9,887
  • 8
  • 44
  • 77
Simon Ryan
  • 212
  • 1
  • 9
  • Are you looking for something similar to "IN" functionality? http://stackoverflow.com/questions/959752/where-in-clause-in-linq – Robert Harvey Apr 10 '13 at 14:47
  • Well yes, but I have no idea how to integrate that into the predicate code in the example. – Simon Ryan Apr 10 '13 at 14:57
  • You have to reverse them. You have to say `listOfCurrentCenterNumbers.Contains(examinerTemp.CentreRegionMaps.CentreNumber);` – Robert Harvey Apr 10 '13 at 14:59
  • Thanks Robert but I've already tried that. I get the following error: "LINQ to Entities does not recognize the method 'Boolean Contains(System.String)' method, and this method cannot be translated into a store expression." – Simon Ryan Apr 10 '13 at 15:12
  • [How to do an “in” query in entity framework](http://stackoverflow.com/questions/1137921/how-to-do-an-in-query-in-entity-framework) – Robert Harvey Apr 10 '13 at 17:06

1 Answers1

0

Would this work? Hard to infer your types...

examinerSpec.Predicate = examinerSpec.AndExpression
    (examinerSpec.Predicate,
        centreTemp =>
            centreTemp.ExaminerCentreAssociations.Any(examinerTemp =>
                examinerTemp.CentreRegionMaps.CentreNumber.Any(cn => cn == currentCentreNumber) &&
                examinerTemp.CentreRegionMaps.Active == true &&
                examinerTemp.DateStart <= DateTime.Now &&
                (!examinerTemp.DateEnd.HasValue || examinerTemp.DateEnd.Value >= DateTime.Now)
            )
    );
Shlomo
  • 14,102
  • 3
  • 28
  • 43
  • Thanks but that gives the error: cannot apply the '==' operator to operands of type char and System.Collections.ObjectModel.Collection on the following statement cn => cn == currentCentreNumber. It thinks cn is of type char! – Simon Ryan Apr 10 '13 at 15:39
  • What's CentreNumber? a string? – Shlomo Apr 10 '13 at 15:52