c# - 根据 List 中的字符串值进行过滤表中的列 Entity Framework Core
问题描述
我在 PostgreSQL 中有一个具有以下结构的表(使用 Entity Framework Core 的代码优先方法)
public class Product_Order
{
[Key]
public string product_number { get; set; }
public string customer_product_number { get; set; }
public List<string> product_statuses { get; set; }
public bool is_test { get; set; } = false;
public DateTime created_at { get; set; } = DateTime.UtcNow;
public DateTime updated_at { get; set; } = DateTime.UtcNow;
public string created_by { get; set; } = "system";
public string updated_by { get; set; } = "system";
}
现在,product_statuses 列通常包含状态列表 - 就绪、已取货、已安排、已关闭、已取消。
我需要想出一个解决方案,它会返回一个产品订单列表,其中不包含已关闭或取消的订单。
这是我目前没有按预期过滤的解决方案
_context.Product_Order.Where(t => t.is_test && !t.statuses.Contains("closed") && !t.statuses.Contains("cancelled")).ToList();
解决方案
我认为您的代码可以让您的数据结构找到该信息。我创建了一个虚拟类和列表来复制您的数据和列表。而且我能够通过使用您的代码找到数据。下面给出了我测试过的示例代码=>
void Test()
{
List<Product_Order> items = new List<Product_Order>();
var temp = new Product_Order() { product_number = "001", isTest = true };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("good");
temp.product_statuses.Add("greate");
temp.product_statuses.Add("new");
items.Add(temp);
temp = new Product_Order() { product_number = "002", isTest = true };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("good");
temp.product_statuses.Add("bad");
temp.product_statuses.Add("notnew");
items.Add(temp);
temp = new Product_Order() { product_number = "003", isTest = true };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("n/a");
temp.product_statuses.Add("bad");
temp.product_statuses.Add("Closed");
items.Add(temp);
temp = new Product_Order() { product_number = "004", isTest = false };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("n/a");
temp.product_statuses.Add("bad");
temp.product_statuses.Add("Cancelled");
items.Add(temp);
var finalOutput = items.Where(c => c.isTest == true && !c.product_statuses.Where(v => v.ToLower() == "closed").Any() && !c.product_statuses.Where(v => v.ToLower() == "cancelled").Any()).ToArray();
}
public class Product_Order
{
public string product_number { get; set; }
public bool isTest { get; set; }
public List<string> product_statuses { get; set; }
}
最后,我认为你的数据与你的 lambda 表达式不相符。所以,我为你做了一点修改。这就是
最终答案:
var finalOutput = _context.Product_Order.Where(c => c.isTest == true && !c.product_statuses.Where(v => v.ToLower() == "closed").Any() && !c.product_statuses.Where(v => v.ToLower() == "cancelled").Any()).ToArray();
请检查我的代码并告诉我。
推荐阅读
- node.js - AWS Lambda:如何在 node.js 中使用 json 正文响应 http 请求
- bash - Imagemagick偶尔意外结束文件
- c# - 在 C# 中使用片段解析相对 URI 引用
- javascript - 使用 javascript 和 Tampermonkey 在 Input 中模拟 Enter 键
- laravel - 缺少必需的参数
- python - Cron 作业运行,但不记录到文件,也不将数据输出到 .csv
- python - python中的json列表操作
- postgresql - Postgresql 备份文件恢复失败
- python - 如何从熊猫数据框中获取最后 15 个值的总和
- nuxt.js - nuxt-link 在无序列表中不起作用