🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.pl.comp.lang.csharp
Article #22410

Re: Linq + filter + lambda

#22410
From: Łukasz 'Maly' Ostrowski
Date: Fri, 13 Jul 2012 20:08
45 lines
2442 bytes
On Fri, 13 Jul 2012 18:12:29 +0200, skynet wrote:
> public static IEnumerable<T> Like<T>(this IEnumerable<T> ListFilter, string 
> PropertyFilter, string FilterValues)
>        {
>            ParameterExpression p = Expression.Parameter(typeof(T), 
> PropertyFilter);
>            var methodCall = Expression.Call(
>                Expression.Constant(FilterValues, typeof(string)),
>                typeof(string).GetMethod("Contains"),
>                Expression.PropertyOrField(p, PropertyFilter)
>            );
>            return ListFilter.Where(Expression.Lambda<Func<T, 
> bool>>(methodCall, p).Compile());
>        }
> 
> Wywołanie to (i to działa)
> objBidn.DataSource = prac.Like <pracownik>("Nazwisko", "Kowalski");

Przekręciłeś kolejność argumentów contains, zrobiłeś Expression który
robi:
FilterValues.Contains(PropertyFilter);

Dlatego "Kowalski".Contains("Kowalski") działa
ale "Kow".Contains("Kowalski") już nie.
;-).

Szybki switch i cyk:

public static IEnumerable<T> Like<T>(this IEnumerable<T> ListFilter, string
PropertyFilter, string FilterValues)
{
    ParameterExpression p = Expression.Parameter(typeof(T),
PropertyFilter);
    var methodCall = Expression.Call(
       Expression.PropertyOrField(p, PropertyFilter), 
       typeof(string).GetMethod("Contains"), 
       Expression.Constant(FilterValues, typeof(string)));
    return ListFilter.Where(Expression.Lambda<Func<T, bool>>(methodCall, p)
.Compile());
}


-- 
Pozdrawiam,
Łukasz 'Maly' Ostrowski.      http://l3v.pl/
Technical Details
Message-ID: <1do9gul9b8wca$.11qpbwn07jo9l.dlg@40tude.net>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!opal.futuro.pl!news.task.gda.pl!news.dialog.net.pl!not-for-mail
References: <jtp3c5$ngp$1@news.task.gda.pl> <1ubd8djr158m6$.co3g7zldp3br.dlg@40tude.net> <jtphae$uqj$1@news.task.gda.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!opal.futuro.pl!news.task.gda.pl!news.dialog.net.pl!not-for-mail
From: Łukasz 'Maly' Ostrowski <maly@USUNnemo.pl>
Newsgroups: alt.pl.comp.lang.csharp
Subject: Re: Linq + filter  + lambda
Date: Fri, 13 Jul 2012 22:08:59 +0200
Organization: Acme
Lines: 45
Message-ID: <1do9gul9b8wca$.11qpbwn07jo9l.dlg@40tude.net>
References: <jtp3c5$ngp$1@news.task.gda.pl> <1ubd8djr158m6$.co3g7zldp3br.dlg@40tude.net> <jtphae$uqj$1@news.task.gda.pl>
NNTP-Posting-Host: static-78-9-71-129.ssp.dialog.net.pl
Mime-Version: 1.0
X-Trace: news.dialog.net.pl 1342210141 13413 78.9.71.129 (13 Jul 2012 20:09:01 GMT)
X-Complaints-To: abuse@dialog.net.pl
NNTP-Posting-Date: Fri, 13 Jul 2012 20:09:01 +0000 (UTC)
User-Agent: 40tude_Dialog/2.0.15.1
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit