tags:

views:

181

answers:

2

I use this to get a list of materials from my database....

  public IQueryable<MaterialsObj> FindAllMaterials()
    {
       var materials =  from m in db.Materials
           join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
           select new MaterialsObj()
               {
                   Id = Convert.ToInt64(m.Mat_id),
                   Mat_Name = m.Mat_Name,
                   Mes_Name = Mt.Name,
               };
        return materials;
    }

But i have seen in an example that has this,

public IQueryable<MaterialsObj> FindAllMaterials()
{
   return from m in db.Materials
       join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
       select new MaterialsObj()
           {
               Id = Convert.ToInt64(m.Mat_id),
               Mat_Name = m.Mat_Name,
               Mes_Name = Mt.Name,
           };
}

Is there a real big difference between the two methods... Assigning my linq query to a variable and returning it... Is it a good/bad practise? Any suggestion which should i use?

+2  A: 

No real difference. In a release / optimized build I would expect the compiler to remove the extra local, anyway. Having the variable is useful if you want to put a breakpoint in and inspect the value prior to return, or if you want to apply additional conditional filtering, for example:

if(applySort) { materials = materials.OrderBy(x => x.Name); }

In your example it doesn't add anything, but it also doesn't cost anything either. Feel free to keep it there; especially if you think it makes the code easier to read.

Marc Gravell
@Marc ya got the point.... well explained....
Pandiya Chendur
+1  A: 

There's no difference, but usually I use the 1st version which makes it easier to set a watch or breakpoint in visual studio if I want to look at the data before it's returned.

Keltex
@Keltex i usually use a variable method to set a breakpoint as you said....
Pandiya Chendur