使用LINQ做一个具有多个值包含 [英] Using Linq to do a Contains with multiple values

查看:658
本文介绍了使用LINQ做一个具有多个值包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 的String [] =名新的字符串[2]; 
名称[0] =阿哌沙班;
名称[1] =西卢;

VAR吃药=(从药物m其中names.Any(m.BrandName.Contains)|| names.Any(m.GenericName.Contains)选择米);



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



修改



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



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

 用于查询经营者的任何不支持的超载。 

下面是我终于结束了。



  VAR吃药=(从db.AdmissionMedications m,其中
(names.Any(N => m.BrandName.Contains(N))|| names.Any( N => m.GenericName.Contains(N))
)选择米);


解决方案

也许财产以后像

  VAR吃药=(从药物
m其中names.Any(名称=> name.Equals(m.BrandName)|| m.GenericName 。载有(名))
选择M);


I have a medication table that I'm looking for certain drug names, but I need to search for multiple names. Here is where I currently am with it.

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);

What I have isn't working, and I'm currently stuck. I know I'm close, but I can't quite figure out what's wrong.

EDIT

For clarification, if the name I'm searching for is desirudin, then the BrandName or Generic name will be longer, so I have to have the contains on the field in the database.

EDIT 2 Here is the error I recieve.

Unsupported overload used for query operator 'Any'.

Here is what I finally ended up with

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

解决方案

Maybe somthing like

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

这篇关于使用LINQ做一个具有多个值包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆