写linq查询获取满足条件的记录
Write linq query to get records satisfying a condition
我需要编写一个 linq 查询以从 (CompRecordPosition == 0 and DPNbr!=0) || (CompRecordPosition!=0)
的索引文件中获取所有记录。我已按如下方式编写了查询,但调试器卡在了这一行而没有进一步处理。请帮助仅获取满足给定条件
的_wIndex
变量的索引记录
private List<WIndex> _wIndex;
private readonly string _FilePath;
internal const string _Key = "Test";
string idxData = File.ReadAllText(Path.Combine(_FilePath, _Key + ".ind"));
_wIndex = JsonConvert.DeserializeObject<List<WIndex>>(idxData);
_wIndex = _wIndex.Where(p2=>_wIndex
.Any(p1 => (p2.CompletionRecordPosition == 0 && p2.WbNewestDrlPmtNbr!=0) ||
p2.CompletionRecordPosition!=0)).ToList();
WIndex Class
public class WIndex
{
public string BaNo;
public long RecordPosition;
public long CompRecordPosition;
public long SegRecordPosition;
public string DataType;
public int RecordIndex;
public Int32 DpNbr;
}
索引文件
[{
"BaNo": "7000650000",
"RecordPosition": 345,
"CompRecordPosition": 567,
"SegRecordPosition": 788,
"DataType": "WELL",
"RecordIndex": 0,
"DPNbr": 0
},
{
"BaNo": "7000790001",
"RecordPosition": 800,
"CompRecordPosition": 0,
"SegRecordPosition": 0,
"DataType": "WELL",
"RecordIndex": 1,
"DPNbr": 810
}]
您尝试执行的 linq 语句中出现了一些问题:
为什么要使用 where
然后立即在 where 子句中使用 any
子句。我的建议是删除 any 子句,因为它不需要从列表中收集您请求的项目。
Any()
- 确定序列中的元素是否存在或满足条件。
Where()
- 根据谓词过滤一系列值。
使声明类似于 _wIndex.Where(x => x.CompletionRecordPosition == 0 && x.WbNewestDrlPmtNbr != 0).ToList();
可能更可取。
此外,.Any(p1 => ...
这里 p1
从未在任何 lambda 表达式中使用或指示。 input parameter
可以删除,因为 p1
和 p2
之间的关系从不在集合之间关联。最有可能导致调试停滞不前,试图确定需要什么。
如果有帮助请告诉我 - 谢谢。
我需要编写一个 linq 查询以从 (CompRecordPosition == 0 and DPNbr!=0) || (CompRecordPosition!=0)
的索引文件中获取所有记录。我已按如下方式编写了查询,但调试器卡在了这一行而没有进一步处理。请帮助仅获取满足给定条件
_wIndex
变量的索引记录
private List<WIndex> _wIndex;
private readonly string _FilePath;
internal const string _Key = "Test";
string idxData = File.ReadAllText(Path.Combine(_FilePath, _Key + ".ind"));
_wIndex = JsonConvert.DeserializeObject<List<WIndex>>(idxData);
_wIndex = _wIndex.Where(p2=>_wIndex
.Any(p1 => (p2.CompletionRecordPosition == 0 && p2.WbNewestDrlPmtNbr!=0) ||
p2.CompletionRecordPosition!=0)).ToList();
WIndex Class
public class WIndex
{
public string BaNo;
public long RecordPosition;
public long CompRecordPosition;
public long SegRecordPosition;
public string DataType;
public int RecordIndex;
public Int32 DpNbr;
}
索引文件
[{
"BaNo": "7000650000",
"RecordPosition": 345,
"CompRecordPosition": 567,
"SegRecordPosition": 788,
"DataType": "WELL",
"RecordIndex": 0,
"DPNbr": 0
},
{
"BaNo": "7000790001",
"RecordPosition": 800,
"CompRecordPosition": 0,
"SegRecordPosition": 0,
"DataType": "WELL",
"RecordIndex": 1,
"DPNbr": 810
}]
您尝试执行的 linq 语句中出现了一些问题:
为什么要使用 where
然后立即在 where 子句中使用 any
子句。我的建议是删除 any 子句,因为它不需要从列表中收集您请求的项目。
Any()
- 确定序列中的元素是否存在或满足条件。
Where()
- 根据谓词过滤一系列值。
使声明类似于 _wIndex.Where(x => x.CompletionRecordPosition == 0 && x.WbNewestDrlPmtNbr != 0).ToList();
可能更可取。
此外,.Any(p1 => ...
这里 p1
从未在任何 lambda 表达式中使用或指示。 input parameter
可以删除,因为 p1
和 p2
之间的关系从不在集合之间关联。最有可能导致调试停滞不前,试图确定需要什么。
如果有帮助请告诉我 - 谢谢。