c# - 如何使用具有字符串 [] 数组属性的 LINQ 实体进行搜索并找到这些包含字符串 [] 数组中的任何字符串的实体?
问题描述
使用 EF Core 2.2,我拥有具有string[]
数组属性的实体,在ApplicationDbContext中,它们被检索到:
modelBuilder.Entity<FruitBasket>()
.Property(e => e.FruitTypes)
.HasConversion(
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries));
例如,一个实体垫在 FruitType 列中包含一个字符串数组:{"Apple", "Banana", "Orange"}
在数据库中保存为:Apple,Banana,Orange
我试图在我的数据库中查找包含输入字符串中的任何字符串的所有对象,比如说:
string[] BasketSearchedFruitTypes = new string[] { "Apple", "Grapefruit", "Pineaple" }
我的 IQueryable:
IQueryable<BasketModel> baskets = GetBasketsQueryable(); //BasketModel contains FruitType string[] prop
要搜索我现在拥有的 LINQ 实体,它说:
if (search.BasketSearchedFruitTypes != null && search.BasketSearchedFruitTypes.Length != 0)
baskets = baskets
.Where(data => search.BasketSearchedFruitTypes
.Any(x => data.FruitType
.Contains(x)));
不幸的是,它没有给我任何回报,我的想法也用完了。
编辑1: 使用表达式后:
baskets = baskets
.Where(data => search.BasketSearchedFruitTypes
.Any(x => data.FruitType
.Contains(x)));
当我尝试将它带到 List<> 时,我得到ArgumentNullException
. 我也无法使用foreach
, .Count()
。我也有:
var result = baskets.Where(data => search.BasketSearchedFruitTypes.Intersect(data.FruitType).Any();
编辑 2:
我刚刚注意到,foreach 循环通过返回的 IQueryable,但在某些时候会中断给ArgumentNullException
. 即使try catch
在循环内部也无济于事......
编辑 3:
实际上,当我将foreach
返回的 IQueryable 放入try catch
时,它是一种临时解决方案,并且工作正常。但我仍然不明白为什么它在枚举时崩溃(循环,而不是循环内的代码)。
解决方案
如果我在您的数据库协议中列出类似的列表,那么这些代码对我有用。
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
class BuilderClass
{
List<BasketModel> baskets;
public BuilderClass()
{
baskets = new List<BasketModel>()
{ new BasketModel { FruitType = new string[] { "Apple", "Grapefruit", "Pineaple", "Bing Cherry", "Cantaloupe" } },
new BasketModel { FruitType = new string[] { "Grapefruit", "Cantaloupe", "Pineaple", "Boysenberries", "Apple" } },
new BasketModel { FruitType = new string[] { "Clementine", "Bing Cherry", "Boysenberries", "Cantaloupe", "Entawak" } },
new BasketModel { FruitType = new string[] { "Entawak", "Grapefruit", "Apple", "Pineaple", "Cantaloupe" } },
new BasketModel { FruitType = new string[] { "Apple", "Pineaple", "Bing Cherry", "Entawak", "Grapefruit" } }
};
}
string[] BasketSearchedFruitTypes = new string[]
{ "Apple", "Grapefruit", "Pineaple" };
public void check()
{
var qbaskets = baskets.AsQueryable();
if (BasketSearchedFruitTypes != null && BasketSearchedFruitTypes.Length != 0)
{
var result = qbaskets.Where(data => BasketSearchedFruitTypes.Any(x => data.FruitType.Contains(x))).ToList();
// result have list with count of 4
}
}
}
class BasketModel
{
public string[] FruitType { get; set; }
}
}
推荐阅读
- sql - 是否可以在 Firebird 的 select 子句中使用布尔表达式?
- python - 尝试用更大的字典更新字典
- python - 我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt)
- r - 闪亮的应用程序不会重现图形,也不是响应式的
- mysql - 如何使用学说删除实体(sql表)?
- c++ - 使用嵌套结构是否相关?
- laravel - 试图在laravel的控制器中获取ajax表单提交的数据
- php - 将 CSS 规则应用于 woocommerce 产品后端页面
- spring - Spring Cloud 应用程序与 AWS Parameter Store 的集成测试
- java - spring-boot-starter-test 中没有 vanilla JUnit 依赖项?