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
C# c linq group by-访问“选择新建”中的其他属性_C#_Linq_Group By - Fatal编程技术网

C# c linq group by-访问“选择新建”中的其他属性

C# c linq group by-访问“选择新建”中的其他属性,c#,linq,group-by,C#,Linq,Group By,我正在转换另一个Type2列表中的分组Type1列表,我想知道如何将Type2对象的属性定义为其他和刚刚定义的Type2属性的串联,在“从分组项中选择新建”中 我的代码如下: List<Type1> list1 = GetList1(); List<Type2> list2 = (from r in list1 group r by new {

我正在转换另一个Type2列表中的分组Type1列表,我想知道如何将Type2对象的属性定义为其他和刚刚定义的Type2属性的串联,在“从分组项中选择新建”中

我的代码如下:

List<Type1> list1 = GetList1();

List<Type2> list2 = (from r in list1
                    group r by new
                    {
                         r.Prop1,
                         r.Prop2,
                         r.Prop3
                    }
                    into groupedRequest
                     select new Type2()
                     {
                         Prop1 = groupedRequest.Key.Prop1,
                         Prop2 = GetProp2FromComplexOperation(groupedRequest.Key.Prop1),
                         Prop3 = Prop1 + Prop2 //<---- The name Prop1 does not exists in the current context
                     }).ToList<Type2>();
但我想避免再次调用GetProp2FromComplexOperation

提前谢谢

您可以这样使用let:

List<Type2> list2 = (from r in list1
                    group r by new
                    {
                         r.Prop1,
                         r.Prop2,
                         r.Prop3
                    }
                    into groupedRequest
                    let p1 = groupedRequest.Key.Prop1
                    let p2 = GetProp2FromComplexOperation(groupedRequest.Key.Prop1)
                     select new Type2()
                     {
                         Prop1 = p1,
                         Prop2 = p2,
                         Prop3 = p1 + p2
                     }).ToList<Type2>();
List<Type2> list2 = (from r in list1
                    group r by new
                    {
                         r.Prop1,
                         r.Prop2,
                         r.Prop3
                    }
                    into groupedRequest
                    let p1 = groupedRequest.Key.Prop1
                    let p2 = GetProp2FromComplexOperation(groupedRequest.Key.Prop1)
                     select new Type2()
                     {
                         Prop1 = p1,
                         Prop2 = p2,
                         Prop3 = p1 + p2
                     }).ToList<Type2>();