|
我有一个,我想查询(通过LINQ)一个表以查找包含“文本”列中任何这些字符串的任何项目。. Q* b3 k7 u$ O- F1 H& g( |+ x3 f
尝试过此操作(无效):1 S# R$ F0 d: y( b5 F$ C: V
items = from dbt in database.Items
% o; B; A, Z7 K* m* ^4 ? where (stringList.FindAll(s => dbt.Text.Contains(s)).Count > 0)3 l6 c* L$ A) `! N6 @
select dbt;+ N. H# B0 ~& f* T& A, V% v
查询将类似于:9 w3 U9 [1 x4 }( c! o! T
select * from items where text like '%string1%' or text like '%string2%'$ s0 c5 a0 Z' O- T* e) o
这可能吗?, r f7 A! Y4 z: ?
% ]4 ^& }6 C8 S Q6 s/ H1 z( `7 S
解决方案:5 \: B7 u' z9 i5 x5 [) i- U* w
" D9 y: y: }3 U7 ?" f7 c* ^, g5 X7 T; \! O# s; _
6 Q! M0 m& i* N4 K8 M, \ 查看本文以执行所需的操作:+ x# V+ Y9 d, Q5 c* P! G4 c
http :' P5 y+ x+ G' _! D- g
//www.albahari.com/nutshell/predicatebuilder.aspx
) Q: X# D8 d9 E1 @+ ?& e0 w这就像梦一样。我实质上是剪切并粘贴了他们的代码,然后将其取回来(当然是用我自己的数据方案):$ `$ U s" K! w S. g. T$ ^# Y
SELECT [t0].[Id], [t0].[DateCreated], [t0].[Name] ...
z+ h& \* [, X( ~( YFROM [dbo].[Companies] AS [t0]- V; N- M J6 z2 ^& E. g) p9 A2 ~
WHERE ([t0].[Name] LIKE @p0) OR ([t0].[Name] LIKE @p1)
" |# \+ t' \5 y3 K9 r这是我为概念验证而运行的代码:
$ S+ C$ }- M7 a' v* c+ jusing System;
% M# }6 s* Q$ e1 qusing System.Collections.Generic;
* `; W v- x! u4 s- N) n, |5 g4 \using System.Linq;+ e* q3 k( Q6 T/ I; W
using System.Text;/ m3 \( y M1 d6 @
using System.Linq.Expressions;
0 L& G0 h P3 b0 p- K. {namespace PredicateTest; v# g$ w* ?# u" A" }( A
{
! u% b& U( L$ Y& r' U" Wclass Program6 u& |# r2 m x0 O2 a! L3 A
{
- _5 b% q6 A9 E" x. T static void Main(string[] args) F, O8 g# b" M8 J' S0 ~
{- |* L I$ A* {/ m/ P- ` J! l0 m
DataClasses1DataContext dataContext = new DataClasses1DataContext();
; Q, A. C7 s+ p& V" Q8 p( s Program p = new Program();
0 t; ?; q I- H/ w/ B O/ S% W Program.SearchCompanies("test", "test2");. B& q V; `) M" r
var pr = from pi in dataContext.Companies.Where(Program.SearchCompanies("test", "test2")) select pi;
. e8 U* ], q) }9 b. @+ } }
: Y9 N* J7 G% Y$ b9 C DataClasses1DataContext dataContext = new DataClasses1DataContext();7 h5 _: Q! a/ s0 v5 P, Q
public static Expression> SearchCompanies(
9 c. `& v! N2 K$ | r& a params string[] keywords)
2 C9 N& i6 L' m" [6 Z {2 n( ^- F/ m/ g! ~3 t
var predicate = PredicateBuilder.False();
' F) ?" Y% R$ M3 U/ U8 ^ foreach (string keyword in keywords)
7 b/ M. [8 X' J. k( Z7 G( h {
, x m+ k" {! ?/ O+ L string temp = keyword;
; ?/ j' C4 Z5 Y; f6 ]# ]: [ predicate = predicate.Or(p => p.Name.Contains(temp));3 F x5 @8 U* }3 [! l
}
2 L: d( z) \) `. }' z return predicate;
$ p# h$ L0 u! ^7 b/ w9 b }
, `8 W! B5 C9 I" U" R) N9 l8 D; x}
: W: c9 o8 j. R& H; Kpublic static class PredicateBuilder
; ]( }& E {2 B$ b' f3 O{
p$ s4 x' U1 s" [2 m" C& r# V public static Expression> True() { return f => true; }% F3 ^+ q% w j# V( i
public static Expression> False() { return f => false; }
1 `6 ?0 Z- v8 g" W/ t7 s public static Expression> Or(this Expression> expr1,( U9 Q! [* r) [8 I& o0 Z5 h- F+ A3 V
Expression> expr2)$ n$ |; c9 L' d; n4 ?6 y
{7 d) @+ ]# `. \: O c1 h
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
+ a l( r5 C8 K, x return Expression.Lambda>
5 m2 Y. ]6 W d+ h% f (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
$ U. |- l& V& X' D4 r }8 ]6 v2 u' R9 m5 q( H7 y6 x8 ^- F
public static Expression> And(this Expression> expr1,
) w3 O7 x, [/ w7 W# o7 Z8 T: p Expression> expr2)
+ _. `/ v" \' ^2 e, ~$ R0 L5 a$ _ {
& e- ]! q5 L7 k7 Y. t var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
. b5 r* U: [- c8 w+ {$ m4 s return Expression.Lambda>
7 f5 e+ Y p3 _ (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
" ~0 C7 B0 Z$ h }
% G f# u. j% ]' I4 t% v7 z}- g# o4 ^/ x0 K( E! ]) f
}
% L- m8 c2 K9 p, D& D6 w* A我建议去该网站获取代码和解释。# G9 u1 \+ s! B; k( W: M
(我留下第一个答案,因为如果您需要一个IN语句,它会很好地工作) |
|