【发布时间】:2017-08-24 15:48:24
【问题描述】:
我正在使用 EntityFrameWorkCore 和 Sqlite 编写一个 dotnet 核心 MVC Web 应用程序。
我正在尝试根据查询字符串提供的数据构建动态 where 子句,查询字符串中可能传递了多个键值对,我想将它们全部添加到 where 子句中,我想我可以这样做,但它会返回初始查询中的所有行。
public class Application
{
public Application()
{
Data = new List<Data>();
}
public int ApplicationId { get; set; }
[Required][Display(Name = "Application Name")]
public string Name { get; set; }
public Guid PublicKey { get; set; }
public Guid PrivateKey { get; set; }
public bool HideFromSearch { get; set; }
public DateTime InsertDate { get; set; }
public List<Data> Data { get; set; }
}
public class Data
{
public Data()
{
DataItems = new List<DataItem>();
}
public int DataId { get; set; }
public int ApplicationId { get; set; }
public DateTime InsertDate { get; set; }
public List<DataItem> DataItems { get; set; }
}
public class DataItem
{
public int DataItemId { get; set; }
public int DataId { get; set; }
public string PropertyName { get; set; }
public string PropertyValue { get; set; }
}
代码在哪里
var apps = context.Applications.Include(app => app.Data).ThenInclude(data => data.DataItems).Where(app => app.PublicKey == publicKey);
foreach (var item in Request.Query)
{
apps = apps.Where(q => q.Data.Any(r => r.DataItems.Any(s => s.PropertyName == item.Key && s.PropertyValue == item.Value[0] )));
}
【问题讨论】:
-
你知道请求查询中将要查询的所有列吗
-
试试 LinqKit,您可以在其中构建动态谓词并将其应用于 where 子句。它是开源的。参考这个链接nuget.org/packages/LinqKit
-
很难相信添加的
Where条件不会减少返回的apps的数量。您真的是要过滤包含的 DataItems 吗? -
是的,我只想返回后续查询匹配的DataItems。
-
这是不断出现的过滤
Include问题。不支持。
标签: c# linq sqlite entity-framework-6