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

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

问题描述

我想获得产品实体的集合,其中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.

它看起来像这样(的结果将是其中有单词榨菜OR在描述文本泡菜或津津乐道的任何产品):

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

我已经看了<一个href=\"http://stackoverflow.com/questions/194930/how-do-i-use-linq-containsstring-instead-of-containsstring\"标题=这个问题>这个类似的问题但无法得到它的工作。

推荐答案

既然你想看看搜索包含载于p的描述基本上你需要的,如果它包含在测试中搜索每个值的字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;

这是拉姆达方法C#语法,因为我的VB是不是很大。

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

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

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