LINQ:实体字符串字段包含任何字符串数组 [英] LINQ: Entity string field contains any of an array of strings

查看:51
本文介绍了LINQ:实体字符串字段包含任何字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 Product 实体的集合,其中 product.Description 属性包含字符串数组中的任何单词.

I want to get a collection of Product entities where the product.Description property contains any of the words in a string array.

它看起来像这样(结果将是描述文本中包含芥末或泡菜"或津津有味"一词的任何产品):

It would look something like this (result would be any product which had the word "mustard OR "pickles" OR "relish" in the Description text):

Dim products As List(Of ProductEntity) = New ProductRepository().AllProducts

Dim search As String() = {"mustard", "pickles", "relish"}

Dim result = From p In products _
     Where p.Description.Contains(search) _
     Select p

Return result.ToList

我已经看过this类似的问题,但无法让它发挥作用.

I already looked at this similar question but couldn't get it to work.

推荐答案

既然你想看看 search 是否包含 p 的描述中包含的单词,你基本上需要测试 search 中的每个值是否包含在p的说明

Since you want to see if search contains a word which is contained in the description of p you basically need to test for each value in search if it is contained in the description of p

result = from p in products
           where search.Any(val => p.Description.Contains(val))
           select p;

这是 lambda 方法的 c# 语法,因为我的 vb 不是那么好

This is c# syntax for the lambda method since my vb is not that great

这篇关于LINQ:实体字符串字段包含任何字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆