从通过LINQ +条件的数组c#列表 [英] c# list from an array via LINQ + condition

查看:116
本文介绍了从通过LINQ +条件的数组c#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用LINQ可以我选择的一个数组,其中符合查询条件的最后一个元素?

How, using LINQ can I select an array the last element in which matches the query condition?

例如,这并不工作:​​

For example, this didn't work:

public class Node{
  public var nodeVar;

    public Node(var arg){     //constructor of node
        this.nodeVar = arg;
    }
}   //end of class


Node[][] path = new Node[3][]; //a jagged array from which to select the required arrays
path[0] = new Node[]{ new Node("A"), new Node("B"), new Node("C") };
path[1] = new Node[]{ new Node("D"), new Node("E"), new Node("W") };
path[2] = new Node[]{ new Node("G"), new Node("W") };

//trying to initialize a list of Node arrays using LINQ:
List<Node[]> thirdArray = path.Select(o => (o.Last().nodeVar == "W") as List<Node[]> ).ToList()

thirdArray出来为空,因为我最有可能使用不正确的选择。
我也得到一个错误:

thirdArray comes out as null, since I am most likely not using Select properly. I am also getting an error:

CS 0039:不能'BOOL'类型转换为System.Collections.Generic.List&LT;节点[]&GT;通过一个内置的转化

我想选择从路径的第二和第三个阵列,并使得从他们的列表(因为在两个第三/第二阵列,最后一个元素的变量W的值)

I would want to select the second and third arrays from the path, and make a list from them (since in both third/second arrays, the last element's variable has a value of W)

推荐答案

您需要一个其中,

var thirdArray = path.Where(o => o.Last().nodeVar=="W"   ).ToList();

这篇关于从通过LINQ +条件的数组c#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆