21

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);
2
  • Please post the error message if you get one, or the fact that it runs without returning results Mar 5, 2013 at 23:04
  • You need to use a lambda in the Any() method, like in my answer. Mar 5, 2013 at 23:11

5 Answers 5

36

Maybe somthing like

C# Linq:

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

Extension methods:

List<Medication> meds = Medications
    .Where( med =>
        names.Any( name =>
            name.Equals( med.BrandName ) || med.GenericName.Contains( name )
        )
    )
    .ToList();
3
  • @Jhorra, your query seems fine then. Could you provide some sample data? And what exactly is not working with your query?
    – aush
    Mar 5, 2013 at 23:05
  • Updated my answer with what i think you need
    – sa_ddam213
    Mar 5, 2013 at 23:09
  • Yours came the closest to what I was looking for. I had to tweak it, but it was on the right track and got me what I needed.
    – Jhorra
    Mar 6, 2013 at 22:23
1

I think you want to try:

var query = Medications.Where(m => names.Contains(m.BrandName) || names.Contains(m.GenericName));
1
  • it will compare only single value. what if i need to compare both value at time in and condition.
    – Ankit Mori
    Aug 22, 2018 at 12:11
1

Just do a join between the medication table and the names array.

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

If I've understood your right:

var meds = Medications.Where(m => names.Contains(m.BrandName) || names.Contains(m.GenericName));
0
0
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"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.