c# - LINQ 连接谓词 OR
问题描述
嗨我正在尝试连接一个 linq 表达式 例如:我有一个 List<string[]>
需要在循环中读取这个的地方我需要创建一个这样的查询
from table where (name ='someone' && id='123') || (name ='another one' && id='223') || ( name='noone' && id='456')
以下代码是我正在处理的
foreach (var item in data)
{
var name= item[4];
var cnpj = item[1];
Expression<Func<IncidentIntegration, bool>> predicated = (x => (x.EmployeesBase.name== name && x.Branch.id== id));
query = query.Union(query.Where(predicated));
}
但它正在创建这样的查询
from table where (name ='someone' || name ='another one' || name='noone') && ( id='223' || id='123' || id='456')
有什么办法可以连接这个吗?
解决方案
我想它可以帮助你
如果我们将 CompleteInfos 视为您的表格,那么:
public class CompleteInfos
{
public int Id { get; set; }
public string Name { get; set; }
public string prop1 { get; set; }
public string prop2 { get; set; }
}
public class Info{
public int Id { get; set; }
public string Name { get; set; }
}
List<CompleteInfos> Table = new List<CompleteInfos>();
// List contains your namse and ids
List<Info> infos = new List<Info>(){
new Info(){Id = 123 , Name = "someone"},
new Info(){Id = 223 , Name = "another"},
new Info(){Id = 456 , Name = "noone"}
}
foreach(var info in infos)
{
List<CompleteInfos> selectedInfo = Table.Where(x => x.Id == info.Id || x.Name == info.Name).ToList();
//selectedInfo is the list in which you can find all item that have your desired id and name
}
推荐阅读
- python-3.x - 估计器 RegressorChain 的无效参数 learning_rate(base_estimator=XGBRegressor
- reactjs - Material UI FormGroup/FormControl 控制的表单登录名/密码不会开始为空
- machine-learning - 在谷歌云平台上运行 jupyter lab 时出现错误 524
- python - 为什么在使用神经网络的多类分类中某些类的敏感性为零?
- python - pandas 数据框中的匹配(fuzzywuzzy)
- server - 3proxy设置内部和外部IP地址不起作用
- java - 缩放数据时使用 L2 LogisticRegression 系数在 Java 中手动计算
- pdf - 电子:窗口['pdfjs-dist/build/pdf.hjs'] 未定义
- django - 覆盖 Django Material Admin 模板并将其保存在项目文件夹中
- javascript - 无法仅选择单个材质 ui 复选框