Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# 使用Linq在列表中以逗号分隔的值中查找值_C#_Linq_List - Fatal编程技术网

C# 使用Linq在列表中以逗号分隔的值中查找值

C# 使用Linq在列表中以逗号分隔的值中查找值,c#,linq,list,C#,Linq,List,我有一个文件类,每个文件都有行列表。每行代码都有逗号分隔的值。我需要找出逗号分隔的数据中是否存在特定的值 public class File { public List<Row> Rows { get; set; } } public class Row { public string Code{ get; set; } } 公共类文件 { 公共列表行{get;set;} } 公共类行 { 公共字符串代码{get;set;} } 这里的代码有逗号表示的值,如ab

我有一个文件类,每个文件都有行列表。每行代码都有逗号分隔的值。我需要找出逗号分隔的数据中是否存在特定的值

public class File
{
    public List<Row> Rows { get; set; }
}

public class Row
{
    public string Code{ get; set; }
}
公共类文件
{
公共列表行{get;set;}
}
公共类行
{
公共字符串代码{get;set;}
}
这里的代码有逗号表示的值,如abc、def、ghi | | xyz、ghj、klm


如果只需要布尔值,我需要使用Linq从文件中的行列表中选择包含abc代码的行:

var isPresent = listOfRows.Any(r => r.Code.Split(",").Any(s => (---your condition---)));
如果希望代码属性值与条件匹配的行:

var rows = listOfRows.Where(r => r.Code.Split(",").Any(s => (---your condition---)));
如果只需要代码属性值:

var values = listOfRows.Select(r => r.Code.Split(",")).Where(s => (---your condition---));
尝试:


使用
String.Split
Enumerable.Any

string value = "search_me";
bool hasValue = file.Rows.Any(fr => fr.Code.Split(',').Any(v => v == value));
包含

file.Rows.Any(fr => fr.Code.Split(',').Contains(value));
更新(根据您的编辑):如果要获取包含此代码的第一行

Row row = file.Rows.FirstOrDefault(fr => fr.Code.Split(',').Contains(value));
if(row != null)
{
    // ...
}
或所有行:

IEnumerable<Row> allRows = file.Where(fr => fr.Code.Split(',').Contains(value));
foreach(Row row in allRows)
{
    // ...
}
IEnumerable allRows=file.Where(fr=>fr.code.Split(',).Contains(value));
foreach(所有行中的行)
{
// ...
}

对结果数组使用
string.Split
然后使用LINQ
任意
扩展方法。
IEnumerable<Row> allRows = file.Where(fr => fr.Code.Split(',').Contains(value));
foreach(Row row in allRows)
{
    // ...
}