Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
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? – Chris 1 hour ago
How is it 'not correct'? What's the error? – Lee 1 hour ago
What is wrong with it? Does it throw an exception or you are concerned with code quality? – Alexandr Mihalciuc 1 hour ago
Is it because human.Gender? is that supposed to be part of the WrappedEntity? – Bob Vale 1 hour ago
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. – Gyuzal Rakhmayeva 1 hour ago

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
Is there any other way? Because I do not have base type of SqlFemaleScreening, SqlMaleScreening, SqlChildScreening adapters. – Gyuzal Rakhmayeva 53 mins ago
1  
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... – Chris 51 mins ago
Right, thanks! Now it works, but without SelectMany:return this.WrappedEntity.human_screen.Select<human_screen, Screening>(e => {} – Gyuzal Rakhmayeva 21 mins ago

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

human_screen.SelectMany(e => e.Some_Collection)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.