c# - LINQ Query with method syntax
问题描述
My requirement is to make boolean value (IsPC=true) only if I found any value with IsCurrent = true from the list and second condition is to filter the list with G or W codes and third condition is to check the PCBNumber length ==15 with only one from the list.
How short can i able to reduce the below query using LINQ method syntax below is my query
var CurrentQ= p.List.Where(x => x.IsConCurrent== true);
if (CurrentQ.Count() > 0)
{
var NCurrentQwithWorQ = p.List.Where(x => x.Codes == Codes.W|| x.Codes== Codes.Q).Count();
if (NCurrentQwithWorQ != null)
{
var PCBNumber = p.List.Where(x => x.PCBNumber .Length == 15).Count();
if (PCBNumber == 1)
{
isPC = true;
}
}
}
解决方案
You can use all conditions in same query like below,
var PCBNumber= p.List.Where(x => x.IsConCurrent== true && (x.Codes == Codes.W|| x.Codes== Codes.Q) && x.PCBNumber.Length == 15);
if (PCBNumber !=null && PCBNumber.Count() == 1)
{
isPC = true;
}
推荐阅读
- python - 如何在没有循环的熊猫系列中通过索引搜索数据
- python - 在 Python 的大型数组中寻找“打印”位置?
- excel - VBA - 设置 Excel 工作簿默认另存为名称
- python - Python 3中的双行循环搜索?
- r - 使用 dplyr 以根据另一个 df 更改列值
- slack-api - Slack API chat.postMessage URL 展开
- c# - 无法使用 Godaddy 专业电子邮件帐户从 asp.net 发送电子邮件
- javascript - Discord.js / Javascript | 我们如何让机器人使用一些过滤器发送相同的消息
- javascript - 在推送到数组时对项目进行排序
- html - 通过其他固定位置 DIV 的 HEIGHT 调整 DIV 元素的 MARGIN-TOP 值(由浏览器窗口宽度更改)