首页 > 解决方案 > 如何使用具有字符串 [] 数组属性的 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时,它是一种临时解决方案,并且工作正常。但我仍然不明白为什么它在枚举时崩溃(循环,而不是循环内的代码)。

标签: c#arrayslinqiqueryableef-core-2.2

解决方案


如果我在您的数据库协议中列出类似的列表,那么这些代码对我有用。

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

推荐阅读