2

I'd appreciate if someone could advise on the following: I need to select different values (in my case Adapters) based on different conditions, I tried like this:

return this.WrappedEntity.human_screen.SelectMany(e => e).Select(e =>
                {
                    AHuman human = _unitOfWork.HumansRepo.GetById(e.human_uid.ToString());
                    if (e.vid_screen == "1" && human.Gender== Gender.Female)
                    {
                        return new SqlFemaleScreening(e);
                    }
                    else if (e.vid_screen == "1" && human.Gender== Gender.Male)
                    {
                        return new SqlMaleScreening(e);
                    }
                    else
                    {
                        return new SqlChildScreening(e);
                    }
                });

But I get the following error:

ERROR: Type arguments for method "System.Linq.Enumerable.SelectMany <TSource,TResult> (System.Collections.Generic.IEnumerable <TSource>, System.Func <TSource, int, System.Collections.Generic.IEnumerable <TResult>>) "should be defined for use. Try to clearly define the type arguments.

Thanks a lot in advance!

5
  • I assume that when you say "it is not correct" you mean that there is a compiler error of some kind? What does it say? Commented Jul 12, 2013 at 8:12
  • How is it 'not correct'? What's the error? Commented Jul 12, 2013 at 8:12
  • What is wrong with it? Does it throw an exception or you are concerned with code quality? Commented Jul 12, 2013 at 8:12
  • Is it because human.Gender? is that supposed to be part of the WrappedEntity? Commented Jul 12, 2013 at 8:13
  • ERROR: Type arguments for method "System.Linq.Enumerable.SelectMany <TSource,TResult> (System.Collections.Generic.IEnumerable <TSource>, System.Func <TSource, int, System.Collections.Generic.IEnumerable <TResult>>) "should be defined for use. Try to clearly define the type arguments. Commented Jul 12, 2013 at 8:17

2 Answers 2

4

The problem is that because you are returning multiple different types of objects the compilers isn't sure what objet type you are expecting in your returned enumerable. Usually when you use something like Select or SelectMany the compiler can work it out so you don't need to worry about it. In this case you need to worry about telling it what they should be.

Your code will be changed to look like:

return this.WrappedEntity.human_screen.SelectMany(e => e).Select<TSource, TResult>(e =>
     {
         //Same code as before in here
     });

TSource should be the type of e in your select method. TResult should be the base type of SqlFemaleScreening, SqlMaleScreening, SqlChildScreening.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any other way? Because I do not have base type of SqlFemaleScreening, SqlMaleScreening, SqlChildScreening adapters.
What return type are you expecting then? Obviously you get an IEnumerable<T> out of these things and what you need to do is define what T is in a way that all the classes can fit into it. If they have no common type then I'm not sure what the benefit of putting them all in the same enumerable is (and the names really do suggest they should have a common type since they sound really rather similar...
Right, thanks! Now it works, but without SelectMany:return this.WrappedEntity.human_screen.Select<human_screen, Screening>(e => {}
0

Result of SelectMany expression should be IEnumerable<T>, e.g.

human_screen.SelectMany(e => e.Some_Collection)

Comments

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.