C# 如何过滤linq查询
我能够使用以下两个参数id1和id2过滤数据,并获得10条记录的准确结果,其中9条记录的价格类型为cs,另一条记录的价格类型为ms 但是,如果我将price_type添加到参数id1和id2 id1=23456567890&id2=6782345&price_type=ms,我将获得3000条记录,而不是一条记录 我在代码中遗漏了什么。任何帮助都将不胜感激C# 如何过滤linq查询,c#,linq,entity-framework,linq-to-sql,C#,Linq,Entity Framework,Linq To Sql,我能够使用以下两个参数id1和id2过滤数据,并获得10条记录的准确结果,其中9条记录的价格类型为cs,另一条记录的价格类型为ms 但是,如果我将price_type添加到参数id1和id2 id1=23456567890&id2=6782345&price_type=ms,我将获得3000条记录,而不是一条记录 我在代码中遗漏了什么。任何帮助都将不胜感激 var data = db.database_BWICs.AsQueryable(); var filter
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);
}
因为并没有添加要限制的筛选器,所以每个筛选器都会将数据添加到结果中 这意味着你在你的过滤器之间,而不是和 您对contains的使用看起来也很奇怪:您使用的是String.contains,而我猜您想要查看列表=>Enumerable.contains中是否有值可能是错误的 你应该在没有过滤数据的情况下做这样的事情 编辑 好吧,如果你想混合和或条件,你可以选择 那么,您的代码应该是要测试的代码
//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);
如果单独查询id1和id2参数,会得到什么结果?您好,谢谢您的回复。当我分别运行id1和id2查询时,我得到了正确的数据记录。api/test?id1=5683028 api/test?id2=7982377。非常感谢您的回复。我已经厌倦了使用与上述相同的方法,并且在参数中多次使用contains,给了我“无数据错误”亲爱的@Raphael,如果你有时间,请你看看这个问题,我仍然遇到数据过滤问题。感谢您的时间和帮助。@user3070072无数据错误是从哪里得到的?你能试着一步一步地调试吗?我已经调试了很多次了,我总是得到相同的错误{System.Data.Entity.DbSet},并且你的代码建议代码落在id1的数据变量上。请在你的问题中发布异常的完整堆栈跟踪,在评论中很难理解这一点。
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));
}
//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);