Is it possible to search in array by word using linq ?
Example:
My array looks like:
AA BB CC DD EE
BB CC DD EE FF
AA BB CC DD EE
I want to return lines where first column is "AA".
I am using linq to sort my array by first column:
sorted = array.OrderBy(o => o[1]).ThenBy(t => t[1]).ToArray();
I try to create somethink like Find an item in List by LINQ?
string search = "AA";
sorted = array.Single(s => s == search);
But it will not work for me beacuse I am using a 2D array.
I'd like to return an array like:
AA BB CC DD EE
AA BB CC DD EE
一个简单的where
就可以了:
var result = array.Where(inner => inner.FirstOrDefault() == "AA");
Try something like that;
var newArray = array.Where(x => x.Length > 0 && x[0] == "AA").ToArray();
Also, you should consider the subarray length to prevent unexpected out of index error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.