早安,
我已经创建了一个结构,作为我已经拥有的类的减少版本.我试图使用linq查询迭代MyClass列表并根据结果创建一个List(基于MyClass上的一些属性的discinct列表).像这样......
List<MyStructure> thisList = (from MyClass thisClass in List<MyClass>
select thisClass.Property1, thisClass.Property2, thisClass.Property3 Distinct.Cast<MyStructure>()).ToList<MyStructure>();
Run Code Online (Sandbox Code Playgroud)
其中MyStructure包含Property1,Property3和Property3的3个变量,具有相同的类型.
我完全清楚上面的代码不会编译,但这是我想要做的那种.有人可以告诉我,这是否可行?
谢谢
如果您想使用现有产品MyStructure,只需使用以下产品:
List<MyStructure> thisList = myClassList.Distinct()
.Select(c => new MyStructure
{
Property1 = c.Property1,
Property2 = c.Property2,
Property3 = c.Property3
}).ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4999 次 |
| 最近记录: |