Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Lambda表达式LINQ中的条件选择_C#_Linq_Lambda - Fatal编程技术网

C# Lambda表达式LINQ中的条件选择

C# Lambda表达式LINQ中的条件选择,c#,linq,lambda,C#,Linq,Lambda,如果有人能提供以下建议,我将不胜感激: 我需要根据不同的条件在我的案例适配器中选择不同的值,我尝试如下: return this.WrappedEntity.human_screen.SelectMany(e => e).Select(e => { AHuman human = _unitOfWork.HumansRepo.GetById(e.human_uid.ToString());

如果有人能提供以下建议,我将不胜感激: 我需要根据不同的条件在我的案例适配器中选择不同的值,我尝试如下:

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);
                    }
                });
但我得到了以下错误:


错误:方法System.Linq.Enumerable.SelectMany的类型参数 System.Collections.Generic.IEnumerable, 应定义System.Func以供使用。试着清楚地定义 类型参数


提前多谢

问题在于,由于您要返回多个不同类型的对象,编译器无法确定您在返回的可枚举对象中所期望的对象类型。通常,当您使用诸如Select或SelectMany之类的工具时,编译器可以计算出来,因此您无需担心。在这种情况下,你需要担心告诉它它们应该是什么

您的代码将更改为:

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

TSource应该是select方法中的e类型。TResult应该是SqlFemaleScreening、SqlMaleScreening、SqlChildScreening的基本类型。

问题在于,由于您返回多个不同类型的对象,编译器无法确定您在返回的可枚举对象中期望的对象类型。通常,当您使用诸如Select或SelectMany之类的工具时,编译器可以计算出来,因此您无需担心。在这种情况下,你需要担心告诉它它们应该是什么

您的代码将更改为:

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

TSource应该是select方法中的e类型。TResult应该是SqlFemaleScreening、SqlMaleScreening、SqlChildScreening的基类型。

SelectMany表达式的结果应该是IEnumerable,例如


SelectMany表达式的结果应为IEnumerable,例如


我假设当你说它不正确时,你的意思是有某种编译器错误?它说什么?怎么是“不正确”?怎么了?怎么了?它会引发异常还是您关心代码质量?是因为人类性别?这应该是Wrappedential的一部分吗?错误:应该定义System.Linq.Enumerable.SelectMany System.Collections.Generic.IEnumerable方法的类型参数,System.Func才能使用。尝试清楚地定义类型参数。我假设当你说它不正确时,你的意思是有某种编译器错误?它说什么?怎么是“不正确”?怎么了?怎么了?它会引发异常还是您关心代码质量?是因为人类性别?这应该是Wrappedential的一部分吗?错误:应该定义System.Linq.Enumerable.SelectMany System.Collections.Generic.IEnumerable方法的类型参数,System.Func才能使用。尝试清楚地定义类型参数。还有其他方法吗?因为我没有SqlFemalescrening、SqlMaleScreening、SqlChildScreening适配器的基类型。那么您期望的返回类型是什么?很明显,你从这些东西中得到了一个IEnumerable,你需要做的是定义什么是T,以一种所有类都能适应它的方式。如果它们没有公共类型,那么我不确定将它们全部放在同一个枚举表中的好处是什么,而且名称确实建议它们应该有一个公共类型,因为它们听起来非常相似……对,谢谢!现在它可以工作了,但是没有SelectMany:返回this.WrappedEntity.human_screen.Selecte=>{}还有其他方法吗?因为我没有SqlFemalescrening、SqlMaleScreening、SqlChildScreening适配器的基类型。那么您期望的返回类型是什么?很明显,你从这些东西中得到了一个IEnumerable,你需要做的是定义什么是T,以一种所有类都能适应它的方式。如果它们没有公共类型,那么我不确定将它们全部放在同一个枚举表中的好处是什么,而且名称确实建议它们应该有一个公共类型,因为它们听起来非常相似……对,谢谢!现在它可以工作了,但是没有SelectMany:返回this.WrappedEntity.human_screen.Selecte=>{}