简体   繁体   中英

How to write query in Entity Framework with conditional multiple where condition?

I am creating a wcf application which is connecting to DB to get some data for customer using Entity Framework. The concept is to search a customer based on the search parameters. User can provide all or few or at least one of the search parameters. But I am quite new in Entity Framework and getting confused on how to do this. I can do this in traditional SQL coding by considering If - Else condition in c# side.

This is my code which is getting the all of the paramters:

   var customers = from o in natCustomer.CustomerLists
                    select o;

    customers = customers.Where(c => c.Name == sName && c.Age == iAge
        && c.Gender == sGender && c.Height == dHeight && c.Weight == dWeight                             
        && c.Nationality == sNationality
        && c.EyeColor == sEyeColor && c.SpecialMark == sSpecialMark);

Please help me by suggesting how do I get the result with few or one parameter only. Thanks

Entity Framework queries are "deferred" queries. They don't actually run until you start asking for results. This means you can build up a query in pieces and it will (mostly) work exactly like one bigger query.

In your case, you can do something like:

var customers = from o in natCustomer.CustomerLists
                select o;

if (!string.isNullOrEmpty(sName)) 
  customers = customers.Where(c => c.Name == sName);

if (!string.isNullOrEmpty(sNationality)) 
  customers = customers.Where(c => c.sNationality == sNationality);

if (!string.isNullOrEmpty(SpecialMark )) 
  customers = customers.Where(c => c.SpecialMark == SpecialMark);

etc. At the end, when you execute the customers query (for example, call ToList or use a foreach loop) EF will consolidate all of those smaller Where clauses into a single SQL query to run against your data.

Assuming you only want to find customers who match on all non-null parameters, an alternative approach is to include the null checks within the where query, and only compare the parameter to the customer data if the parameter is not null.

customers = customers.Where(c => (string.isNullOrEmpty(sName) || c.Name == sName) 
    && (iAge == null || c.Age == iAge)
    && (string.isNullOrEmpty(sGender) || c.Gender == sGender));

You need some way to determine if the given inputs are set or not. For a simplification I assume you receive your parameters as nullables. So you could add an additional condition, if the parameter is provided:

customers = sName == null ? customers : customers.Where(c => c.Name == sName);
customers = iAge == null ? customers : customers.Where(c => c.Age == iAge);
customers = sGender == null ? customers : customers.Where(c => c.Gender == sGender);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM