c# - 如何在 linq 查询 c# 中的 WHERE 语句后嵌入动态 OR 条件
问题描述
我有一个这样的 LINQ 查询:
var getOnlyMP = from mpPoint in cmList where
mpPoint.Component.Contains("asd") ||
mpPoint.Component.Contains("dsa") ||
mpPoint.Component.Contains("123") ||
mpPoint.Component.Contains("456")
select new MP
{
MPName = mpPoint.Component,
X = mpPoint.PlaceX,
Y = mpPoint.PlaceY,
};
我想将“asd”、“dsa”、“123”、“456”存储在一个数组中,那么是否有可能在该数组上循环并动态比较 WHERE 子句之后的所有项目?
解决方案
您可以.Any()
与数组一起使用,
var expectedStrings = new string[] {"asd", "dsa", "123", "456"};
var getOnlyMP = cmList.Where(mpPoint=>
expectedStrings.Any(x => mpPoint.Component.Contains(x)))
.Select(x => new MP
{
MPName = x.Component,
X = x.PlaceX,
Y = x.PlaceY,
});
在查询形式中,
var expectedStrings = new string[] {"asd", "dsa", "123", "456"};
var getOnlyMP = from mpPoint in cmList where
expectedStrings.Any(x => mpPoint.Component.Contains(x)))
select new MP
{
MPName = mpPoint.Component,
X = mpPoint.PlaceX,
Y = mpPoint.PlaceY,
};
推荐阅读
- javascript - Javascript按字数拆分字符串
- multithreading - 多线程时 System.FormatException 错误
- java - 无法使用 JNA 访问 IActiveDesktop 接口(不支持此类接口(HRESULT:80004002)
- python - 在pygame中触摸特定块时,我不知道如何结束游戏
- reporting-services - 如何在SSRS的折线图中设置最大横轴范围?
- python - 尽管分配了内存,但多处理池仍无法正常工作
- java - 深度复制模板类
- ios - JSONDecoder 无法解码 Array 但可以解码 String
- javascript - Vue.js 跨组件共享输入数据
- python - 如何让 Mypy 在 Callable 中识别类的协议成员资格?