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: ozdotnet-boun...@ozdotnet.com [ozdotnet-boun...@ozdotnet.com] On Behalf Of Michael Minutillo [michael.minuti...@gmail.com] 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 <arjang.ass...@gmail.com<mailto:arjang.ass...@gmail.com>> 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.