{"id":972,"date":"2009-12-08T13:50:45","date_gmt":"2009-12-08T12:50:45","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=972"},"modified":"2009-12-08T13:50:45","modified_gmt":"2009-12-08T12:50:45","slug":"szures-opcionalis-parameterekre-linq-val","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2009\/12\/08\/szures-opcionalis-parameterekre-linq-val\/","title":{"rendered":"Sz\u0171r\u00e9s opcion\u00e1lis param\u00e9terekre LINQ-val"},"content":{"rendered":"<p>A feladv\u00e1ny, hogy van sok sz\u0171r\u00e9si felt\u00e9tel, ezekre sz\u0171rni kell, ha van \u00e9rv\u00e9nyes \u00e9rt\u00e9k\u00fck, vagy kihagyni a sz\u0171r\u00e9sb\u0151l, ha nincs.<br \/>\nA feladatra SQL Server eset\u00e9n bool algebr\u00e1s megold\u00e1st \u00edrn\u00e9k:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nselect * from tabla\r\nwhere \r\n(@p1 is null) or (@p1 = col1)\r\nand\r\n(@p2 is null) or (@p2 = col2)\r\n...\r\noption(recompile)\r\n<\/pre>\n<p>Az option hint az\u00e9rt kell, mert \u00edgy a nullos param\u00e9terek teljesen kiesnek a tervb\u0151l, csak a val\u00f3di sz\u0171r\u00e9sekre k\u00e9sz\u00fcl terv. Ez szerintem \u00f3ri\u00e1si dolog, \u00e9rdemes \u00e9szben tartani.<\/p>\n<p>Ugyanezt a logik\u00e1t linqval is el lehet j\u00e1tszani, ref t\u00edpusokkal kb. \u00edgy:<\/p>\n<p>var x = from z in Valami<br \/>\nwhere (p1 == null) || (p1 = z.col1)<br \/>\n&#038;&#038;<br \/>\n(p2 == null) || (p2 = z.col2)<\/p>\n<p>Egy m\u00e1sik megold\u00e1sban azt haszn\u00e1ljuk ki, hogy k\u00e9sleltetett m\u00f3don \u00e9rt\u00e9kel\u0151dnek ki a kifejez\u00e9sek, \u00edgy lehet \u0151ket l\u00e1ncolni. <a href=\"http:\/\/stackoverflow.com\/questions\/11194\/conditional-linq-queries\">Itt<\/a> l\u00e1that\u00f3 ez a megold\u00e1s, \u00e9s m\u00e9g m\u00e1s megk\u00f6zel\u00edt\u00e9sek is.<\/p>\n<p>A harmadik megold\u00e1sban \u00edrhatunk egy saj\u00e1t <a href=\"http:\/\/stackoverflow.com\/questions\/632434\/linq-to-sql-where-clause-optional-criteria\">sz\u0171r\u0151 oper\u00e1tort<\/a> is erre a c\u00e9lra. Az el\u0151bbi c\u00edmr\u0151l:<\/p>\n<p>public static IQueryable<TSource> WhereIf<TSource>(<br \/>\n    this IQueryable<TSource> source, bool condition,<br \/>\n    Expression<Func<TSource, bool>> predicate)<br \/>\n{<br \/>\n    if (condition)<br \/>\n        return source.Where(predicate);<br \/>\n    else<br \/>\n        return source;<br \/>\n}<\/p>\n<p>Neh\u00e9z megindokolni, melyik megold\u00e1s a jobb.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A feladv\u00e1ny, hogy van sok sz\u0171r\u00e9si felt\u00e9tel, ezekre sz\u0171rni kell, ha van \u00e9rv\u00e9nyes \u00e9rt\u00e9k\u00fck, vagy kihagyni a sz\u0171r\u00e9sb\u0151l, ha nincs. A feladatra&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10,53,4],"tags":[],"class_list":["post-972","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-linq","category-szakmai-elet"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=972"}],"version-history":[{"count":1,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/972\/revisions"}],"predecessor-version":[{"id":975,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/972\/revisions\/975"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}