开发者

Number of Or's based on a list

I'm working on something that queries a database depending on values the user inputs. For instance, the user can search for a country (perhaps 'Au') and any countries containing 'Au' will get returned.

The issue is that I need to be able to add a bunch of 'or' statements to a Predicate to achieve this result, but have no idea how to go about it.

Right now, I have

List<int> ids;
ids = (from d in DTDC.Countries
       where d.Name.ToLower().Contains( country.ToLower() )
       select d.CountryID).ToList();

var SearchPredicate = PredicateBuilder.True<DLC>();

// Example
searchPredicate = searchPred开发者_运维知识库icate.And(c => c.CountryID == 0 || c.CountryID == 1 );

So I need to generate

searchPredicate = searchPredicate.And(c => c.CountryID == 0 || c.CountryID == 1 );

this statement depending on the values in the List that is returned from my LINQ query.

For instance, if the list has the values 0, 1, 7, then the query needs to be

searchPredicate = searchPredicate.And(c => c.CountryID == 0 || c.CountryID == 1 || || c.CountryID == 7 );

Hopefully thats clear enough for someone to understand what I'm trying to do :)


This is a full implementation of In method. You can choose a few of them.

public static class Ext
{
    public static bool In<T>(this T val, params T[] values)
    {
        return val.In(EqualityComparer<T>.Default, values);
    }

    public static bool In<T>(this T val, EqualityComparer<T> comparer, params T[] values)
    {
        foreach (var v in values)
        {
            if (comparer.Equals(val, v)) return true;
        }
        return false;
    }

    public static bool In<T>(this T val, Func<T, T, bool> comparer, params T[] values)
    {
        foreach (var v in values)
        {
            if (comparer(v, val)) return true;
        }
        return false;
    }
}

Usage:

int num = 1;
bool exist = num.In(2, 3, 4, 5);


Not exactly sure what you're trying to do here.. If the list has 3 elements, do you want to generate c => c.CountryID == 0 || c.CountryID == 1 || c.CountryID == 2?

If so, why not just (c.CountryID < ids.Count)?

Also, if you execute a query that only returns 1 country, are you sure you want to filter it on countryID == 0?


Why are you creating a List of it if you want to filter more? Instead return an IQueryable and then you can apply more filters on that... like this for example:

public IQueryable<Country> GetAllCountries()
{
    return from d in DTDC.Countries select d;
}

Then you can have extension methods for IQueryable<Country> like:

public static class CountryFilters
{
    public static IQueryable<Country> ThatContainsName(this IQueryable<Country> query, string country)
    {
        return query.Where(y => y.Name.ToLower().Contains(country.ToLower()));
    }
}

To that you can add more filters, and last when you have filtered your query you can make on that extracts your ids.

Also, when applying those filters I would check that the generated sql is of good quality so you don't end up making multiple queries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜