LINQ to SQL-选择字符串数组之类的文本 [英] LINQ to SQL - select where text like string array

查看:86
本文介绍了LINQ to SQL-选择字符串数组之类的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量计数的List >,我想查询(通过LINQ)一个表,以查找文本"列中包含任何那些字符串的任何项目.

I have a List<string> of variable count, and I want to query (via LINQ) a table to find any items that contain any of those strings in the Text column.

尝试过此操作(无效):

Tried this (doesn't work):

items = from dbt in database.Items
         where (stringList.FindAll(s => dbt.Text.Contains(s)).Count > 0)
         select dbt;

查询将类似于:

select * from items where text like '%string1%' or text like '%string2%'

这可能吗?

推荐答案

查看本文以完成您想要做的事情:
http://www.albahari.com/nutshell/predicatebuilder.aspx

这就像一场梦.我实质上是剪切并粘贴了他们的代码,然后将其取回来(当然是用我自己的数据方案):

Check this article out to do what you want:
http://www.albahari.com/nutshell/predicatebuilder.aspx

This works like a dream. I essentially cut and pasted their code and got this back (with my own data-scheme of course):

SELECT [t0].[Id], [t0].[DateCreated], [t0].[Name] ...
FROM [dbo].[Companies] AS [t0]
WHERE ([t0].[Name] LIKE @p0) OR ([t0].[Name] LIKE @p1)

这是我为概念验证而运行的代码:

Here is the code I ran for the proof of concept:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace PredicateTest
{
class Program
{
    static void Main(string[] args)
    {
        DataClasses1DataContext dataContext = new DataClasses1DataContext();

        Program p = new Program();
        Program.SearchCompanies("test", "test2");
        var pr = from pi in  dataContext.Companies.Where(Program.SearchCompanies("test", "test2")) select pi;
    }

    DataClasses1DataContext dataContext = new DataClasses1DataContext();

    public static Expression<Func<Company, bool>> SearchCompanies(
                                                  params string[] keywords)
    {
        var predicate = PredicateBuilder.False<Company>();
        foreach (string keyword in keywords)
        {
            string temp = keyword;
            predicate = predicate.Or(p => p.Name.Contains(temp));
        }
        return predicate;
    }

}

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                         Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
    }
}
}

我建议去网站获取代码和解释.

I'd suggest going to the site for the code and explanation.

(我留下第一个答案,因为如果需要IN语句,它会很好地工作)

(I am leaving the first answer because it works well if you need an IN statement)

这篇关于LINQ to SQL-选择字符串数组之类的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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