简体   繁体   中英

How to Filter linq query

I am able to filter the data with the following two parameters id1 and id2, and get accurate result of 10 records, from which have 9 with a price_type=cs and other with price-type=ms.

However, if I add price_type to the parameters id1 and id2 (id1=23456,567890&id2=6782345&price_type=ms), I get 3000 records instead of getting one record.

Am I missing something in the code. Any help would be very much appreciated.

        var data = db.database_BWICs.AsQueryable();
        var filteredData = new List<IQueryable<database_Data>>();


        if (!string.IsNullOrEmpty(query.name))
        {
            var ids = query.name.Split(',');
            foreach (string i in ids)
            {
                filteredData.Add(data.Where(c => c.Name != null && c.Name.Contains(i)));
            }
        }

        if (!string.IsNullOrEmpty(query.id2))
        {
            var ids = query.id2.Split(',');
            foreach (string i in ids)
            {
                filteredData.Add(data.Where(c => c.ID2!= null && c.ID2.Contains(i)));
            }
        }

        if (!string.IsNullOrEmpty(query.id1))
        {
            var ids = query.id1.Split(',');
            foreach (string i in ids)
            {
                filteredData.Add(data.Where(c => c.ID1!= null && c.ID1.Contains(i)));
            }
        }

        if (query.price_type != null)
        {
            var ids = query.price_type.Split(',');
            foreach (string i in ids)
            {
                filteredData.Add(data.Where(c => c.Type.Contains(i)));
            }
        }

        if (filteredData.Count != 0)
        {
            data = filteredData.Aggregate(Queryable.Union);
        }

Updated Code:

 var data = db.database_BWICs.AsQueryable();

        if (!string.IsNullOrEmpty(query.name))
        {
            var ids = query.name.Split(',');
            data = data.Where(c => c.Name != null && ids.Contains(c.Name));
         }

        if (query.price_type != null)
        {
            var ids = query.price_type.Split(',');
            data = data.Where(c => ids.Contains(c.Cover));
        }

        if (!String.IsNullOrEmpty(query.id1))
        {
            var ids = query.id1.Split(',');
            data = data.Where(c => c.ID1!= null && ids.Contains(c.ID1));
        }

Because you don't add filter to restrict, every filter adds datas to result.

It means you make OR between your filters, not AND.

And your usage of contains looks rather strange too : you're using String.Contains , while I would guess (maybe wrong) that you want to see if a value is in a list => Enumerable.Contains

You should rather go for something like this (withoud filteredData )

var data = db.database_BWICs.AsQueryable();
if (!string.IsNullOrEmpty(query.name))
{
    var ids = query.name.Split(',');
    data = data.Where(c => c.Name != null && ids.Contains(c.Name)));
}
//etc.

if (query.price_type != null)
{
     var ids = query.price_type.Split(',');
     data = data.Where(c => ids.Contains(c.Type));
}

EDIT

Well, if you wanna mix and or conditions, you could go for PredicateBuilder

Then your code should look like that (to be tested).

//manage the queries with OR clause first
var innerOr = Predicate.True<database_BWICs>();//or the real type of your entity

if (!String.IsNullOrEmpty(query.id1))
{
    var ids = query.id1.Split(',');
    innerOr = innerOr.Or(c => c.ID1!= null && ids.Contains(c.ID1));
}
if (!String.IsNullOrEmpty(query.id2))
{
    var ids = query.id2.Split(',');
    innerOr = innerOr.Or(c => c.ID2!= null && ids.Contains(c.ID2));
}
//now manage the queries with AND clauses
var innerAnd = Predicate.True<database_BWICs>();//or the real type of your entity

if (query.price_type != null)
{
    var ids = query.price_type.Split(',');
    innerAnd = innerAnd.And(c => ids.Contains(c.Type));
}
//etc.

innerAnd = innerAnd.And(innerOr);

var data = db.database_BWICs.AsQueryable().Where(innerAnd);

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