简体   繁体   English

编写 linq 查询获取满足条件的记录

[英]Write linq query to get records satisfying a condition

I need to write a linq query to get all records from index file for which the (CompRecordPosition == 0 and DPNbr!=0) || (CompRecordPosition!=0)我需要编写一个 linq 查询来从索引文件中获取所有记录,其中(CompRecordPosition == 0 and DPNbr!=0) || (CompRecordPosition!=0) (CompRecordPosition == 0 and DPNbr!=0) || (CompRecordPosition!=0) .I have written the query as below but the debugger is getting stuck at this line without proceeding further. (CompRecordPosition == 0 and DPNbr!=0) || (CompRecordPosition!=0) 。我已经编写了如下查询,但调试器卡在这一行而没有进一步处理。 Please help to get only those index records to _wIndex variable satisfying the given condition请帮助仅将那些索引记录获取到满足给定条件的_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 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;
}

Index file索引文件

  [{
    "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
  }]

There's some questions raised in the linq statement you are trying to execute:您尝试执行的 linq 语句中提出了一些问题:

Why use a where then immediately an any clause within the where clause.为什么在 where 子句中使用where然后立即使用any子句。 My recommendation is to eliminate the any clause as its not need to collect items you are requesting from the list.我的建议是删除 any 子句,因为它不需要从列表中收集您请求的项目。

Any() - Determines whether an element of a sequence exists or satisfies a condition. Any() - 确定序列的元素是否存在或是否满足条件。

Where() - Filters a sequence of values based on a predicate. Where() - 根据谓词过滤一系列值。

Making the statement something like _wIndex.Where(x => x.CompletionRecordPosition == 0 && x.WbNewestDrlPmtNbr.= 0);ToList();使语句类似于_wIndex.Where(x => x.CompletionRecordPosition == 0 && x.WbNewestDrlPmtNbr.= 0);ToList(); would likely be something more preferrable.可能会更可取。

Also, .Any(p1 =>... here p1 is never used or indicated later in any of the lambda expressions. The input parameter can be removed as the relation between p1 and p2 never correlates between the sets. Most likely causing the debug to sit and spin trying to determine what is needed.此外, .Any(p1 =>...这里p1从未在任何 lambda 表达式中使用或稍后指示。可以删除input parameter ,因为p1p2之间的关系在集合之间从不相关。很可能导致调试坐着旋转,试图确定需要什么。

Let me know if this helps - thanks.让我知道这是否有帮助 - 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM