0

I have this code:

Strutture = from Ricettivito s in Strutture
            where s.ServiziAttivi.Cast<string>().Intersect(IDSServizi).Count() == IDSServizi.Count()
            select s;

I need to:

  1. Cast ServiziAttivi (which is a list of MyService) into a list of string (which must contains MyService.UniqueID)
  2. Than, check if this list contains each elements of IDSServizi (which is a list of string).

But seems I cannot doint that conversion?

5
  • Only you can tell us why you can't do that conversion - what's the error message? Commented Apr 17, 2013 at 14:20
  • Unable to cast object of type 'Ricettivita.MyService' to type 'System.String'. Commented Apr 17, 2013 at 14:20
  • I need to extract Ricettivito which ServiziAttivi that contains each values inside IDSServizi Commented Apr 17, 2013 at 14:22
  • Rather than casting, try .Select-ing the UniqueID from the ServiziAttivi-list. Commented Apr 17, 2013 at 14:22
  • Check this thread. stackoverflow.com/questions/1407689/… Commented Apr 17, 2013 at 14:30

2 Answers 2

4

First cast to .Cast<Ricettivita.MyService>() then select a string property.

    where s.ServiziAttivi
             .Cast<Ricettivita.MyService>()
             .Select(x=>x.UniqueID).Intersect(IDSServizi).Count()
Sign up to request clarification or add additional context in comments.

1 Comment

If OP did a .ToList() would they need a cast, first? Assuming OPs custom collection class inherits from Enumerable in some way.
1

I think you should use Select instead of Cast:

Strutture = from Ricettivito s in Strutture
            where s.ServiziAttivi.Select(x => (string)x.UniqueID).Intersect(IDSServizi).Count() == IDSServizi.Count()
            select s;

1 Comment

I can't. Check the answer to @Michael Perrenoud

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.