使用LINQ to做一个具有多个值包含(Using Linq to do a Contains wi

2019-08-17 08:14发布

我有我在寻找某种药物名称药物表,但我需要搜索多个名称。 这里是我目前用它。

string[] names = new string[2];
names[0] = "apixaban";
names[1] = "desirudin";

var meds = (from m in Medications where names.Any(m.BrandName.Contains) || names.Any(m.GenericName.Contains) select m);

我有什么不工作,我现在卡住。 我知道我很近,但我不能完全弄清楚什么是错的。

编辑

为了澄清,如果我在寻找的西卢定的名称,则名优产品或通用名称会比较长,所以我必须有包含在数据库中的字段。

编辑2这是我收到的错误。

Unsupported overload used for query operator 'Any'.

以下是我终于结束了

var meds = (from m in db.AdmissionMedications where 
(names.Any(n => m.BrandName.Contains(n)) || names.Any(n => m.GenericName.Contains(n))
) select m);

Answer 1:

也许类似的财产以后

C#中的LINQ:

var meds = (from m in Medications 
            where names.Any(name => name.Equals(m.BrandName) || m.GenericName.Contains(name)) 
            select m);

扩展方法:

List<Medication> meds = Medications
    .Where( med =>
        names.Any( name =>
            name.Equals( med.BrandName ) || med.GenericName.Contains( name )
        )
    )
    .ToList();


Answer 2:

我想你也想尝试:

var query = Medications.Where(m => names.Contains(m.BrandName) || names.Contains(m.GenericName));


Answer 3:

只是不要服药表和名称阵列之间的连接。

var query = from m in Medications
            from n in in names
            where m.BrandNames.Any(bn => bn.Contains(n)) || m.GenericNames.Any(gn => gn.Contains(n))
            select m;


Answer 4:

如果我理解你的权利:

var meds = Medications.Where(m => names.Contains(m.BrandName) || names.Contains(m.GenericName));


Answer 5:

var x = (from c in Reports where c.HKPlanningQty == xi select c).Select(u=>new {Style=u.Style,QTN=u.HKPlanningQty}).OrderBy(u =>u.Style ).Where(v=>v.Style.Contains("44")||v.Style.Contains("58"));


文章来源: Using Linq to do a Contains with multiple values
标签: c# linq contains