如何过滤linq查询

时间:2014-01-14 09:47:29

标签: c# linq entity-framework linq-to-sql

我可以使用以下两个参数id1和id2过滤数据,并获得10条记录的准确结果,其中9条带有price_type = cs,另一条带有price-type = ms。

但是,如果我将price_type添加到参数id1和id2(id1 = 23456,567890& id2 = 6782345& price_type = ms),我会获得3000条记录而不是获得一条记录。

我在代码中遗漏了什么。任何帮助将非常感谢。

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

更新代码:

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

1 个答案:

答案 0 :(得分:0)

因为您没有添加要限制的过滤器,所以每个过滤器都会将数据添加到结果中。

这意味着您在过滤器之间进行OR,而不是AND。

你对contains的使用看起来也很奇怪:你正在使用String.Contains,而我猜(也许是错误的)你想看看一个值是否在list =&gt;中Enumerable.Contains

你应该选择这样的事情(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));
}

修改

好吧,如果你想混合和/或条件,你可以去PredicateBuilder

然后你的代码应该是那样的(待测试)。

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