Intresting, but I was trying to have a most general expression and
restrict it as I went along. This example turns that idea upside down
on it's head.

None the less it is a valid solution, gives a glimpse to a completely
different class of solutions where one might start with the most
restricted state and work it's way back to the least restricted. Maybe
in security related patterns, where the most restriction is applied
and as additional conditions are met the restraints are relaxed.

Regards

Arjang

On 13 July 2010 17:32, Keith Peck <[email protected]> wrote:
> Nice.
>
> how about:
>
> public class Product
> {
>     public List<Product> GetAll()
>     {
>         return GetAllContaining(null);
>     }
>
>     public List<Product> GetAllContaining(string needle)
>     {
>           DataEntities ctx = new DataEntities();
>           {
>             var qry = from c in ctx.Products
>                       where (c.DATAAREAID == "boi" && c.ITEMGROUPID == "fg")
>                         && ((string.IsNullOrEmpty(needle)) ||
> (c.Id.Contains(needle) || c.Name.Contains(needle)))
>                       select c;
>             return qry.ToList();
>         }
>     }
> }
>
>
> K...
>
> ________________________________
> From: [email protected] [[email protected]] On
> Behalf Of Michael Minutillo [[email protected]]
> Sent: Tuesday, 13 July 2010 5:06 PM
> To: ozDotNet
> Subject: Re: Refactoring linq Query Expression
>
> You could do this:
>
> public class Product
> {
>    public List<Product> GetAll()
>    {
>        DataEntities ctx = new DataEntities();
>        {
>            var qry = from c in InnerQuery(ctx)
>                      select c;
>            return qry.ToList();
>        }
>    }
>    public List<Product> GetAllContaining(string needle)
>    {
>        DataEntities ctx = new DataEntities();
>        {
>            var qry = from c in InnerQuery(ctx)
>                      where c.Id.Contains(needle) || c.Name.Contains(needle)
>                      select c;
>            return qry.ToList();
>        }
>    }
>
>    private IQueryable<Product> InnerQuery(DataEntities ctx)
>    {
> return ctx.Products.Where(c => c.DATAAREAID == "boi" && c.ITEMGROUPID ==
> "fg");
>    }
> }
> Multiple WHERE clauses are just ANDed together anyway
> On Tue, Jul 13, 2010 at 3:02 PM, Arjang Assadi <[email protected]>
> wrote:
>>
>> Greetings,
>>
>> I am trying to factor out the qry from GetAll and reuse it (instead of
>> copy and pasting ) in GetAllContaining(needle).
>> Here is the code: (any other points is also welcomed :)
>>
>> public class Product
>> {
>>    public List<Product> GetAll()
>>    {
>>        DataEntities ctx = new DataEntities();
>>        {
>>            var qry = from c in ctx.Products
>>                      where c.DATAAREAID == "boi" && c.ITEMGROUPID == "fg"
>>                      select c;
>>            return qry.ToList();
>>        }
>>    }
>>
>>    public List<Product> GetAllContaining(string needle)
>>    {
>>        DataEntities ctx = new DataEntities();
>>        {
>>            var qry = from c in ctx.Products
>>                      where (c.DATAAREAID == "boi" && c.ITEMGROUPID ==
>> "fg") &&
>>                      (c.Id.Contains(needle) || c.Name.Contains(needle))
>>                      select c;
>>            return qry.ToList();
>>        }
>>    }
>> }
>>
>> Thank you
>>
>> Arjang
>
>
>
> --
> Michael M. Minutillo
> Indiscriminate Information Sponge
> Blog: http://wolfbyte-net.blogspot.com
>
> ________________________________
> NOTICE - This communication is intended only for the person or entity to
> which it is addressed and may contain confidential and/or privileged
> material. Any review, retransmission, dissemination or other use of, or
> taking any action in reliance on, this communication by persons or entities
> other than the intended recipient is prohibited. If you are not the intended
> recipient of this communication please delete and destroy all copies and
> telephone SMS Management & Technology on 1300 842 767 immediately. Any views
> expressed in this Communication are those of the individual sender, except
> where the sender specifically states them to be the views of SMS Management
> & Technology. Except as required by law, SMS Management & Technology does
> not represent, warrant and/or guarantee that the integrity of this
> communication has been maintained nor that the communication is free from
> errors, virus, interception or interference.
>

Reply via email to