<p>I am prototyping some C# 3 collection filters and came across this.
I have a collection of products:</p>

<pre><code>public class MyProduct
{
    public string Name { get; set; }
    public Double Price { get; set; }
    public string Description { get; set; }
}

var MyProducts = new  List&lt;MyProduct&gt;
{            
    new  MyProduct
    {
        Name = "Surfboard",
        Price = 144.99,
        Description = "Most important thing you will ever own."
    },
    new MyProduct
    {
        Name = "Leash",
        Price = 29.28,
        Description = "Keep important things close to you."
    }
    ,
    new MyProduct
    {
        Name = "Sun Screen",
        Price = 15.88,
        Description = "1000 SPF! Who Could ask for more?"
    }
};
</code></pre>

<p>Now if I use LINQ to filter it works as expected:</p>

<pre><code>var d = (from mp in MyProducts
             where mp.Price &lt; 50d
             select mp);
</code></pre>

<p>And if I use the Where extension method combined with a Lambda the filter works as well:</p>

<pre><code>var f = MyProducts.Where(mp =&gt; mp.Price &lt; 50d).ToList();
</code></pre>

<p><strong>Question:</strong> What is the difference, and why use one over the other?</p>

[{'answer_id': 5199, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 4, 'selected': True, 'text': '<p>LINQ turns into method calls like the code you have.</p>\n\n<p>In other words, there should be no difference.</p>\n\n<p>However, in your two pieces of code you are not calling .ToList in the first, so the first piece of code will produce an enumerable data source, but if you call .ToList on it, the two should be the same.</p>\n'}, {'answer_id': 5641, 'author': 'Ricky', 'author_id': 653, 'author_profile': 'https://Stackoverflow.com/users/653', 'pm_score': 0, 'selected': False, 'text': '<p>Other than the ToList difference, #2 is a lot more readable and natural IMO</p>\n'}, {'answer_id': 7741, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': '<p>As mentioned d will be <code>IEnumerable&lt;MyProduct&gt;</code> while f is <code>List&lt;MyProduct&gt;</code></p>\n\n<p>The conversion is done by the C# compiler</p>\n\n<pre><code>var d = \n    from mp in MyProducts\n    where mp.Price &lt; 50d\n    select mp;\n</code></pre>\n\n<p>Is converted to (before compilation to IL and with generics expanded):</p>\n\n<pre><code>var d = \n    MyProducts.\n    Where&lt;MyProduct&gt;( mp =&gt; mp.Price &lt; 50d ).\n    Select&lt;MyProduct&gt;( mp =&gt; mp ); \n    //note that this last select is optimised out if it makes no change\n</code></pre>\n\n<p>Note that in this simple case it makes little difference.  Where Linq becomes really valuable is in much more complicated loops. </p>\n\n<p>For instance this statement could include group-bys, orders and a few let statements and still be readable in Linq format when the equivalent <code>.Method().Method.Method()</code> would get complicated.</p>\n'}, {'answer_id': 13511, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 0, 'selected': False, 'text': '<p>The syntax you are using for <em>d</em> will get transformed by the compiler into the same IL as the extension methods. The "SQL-like" syntax is supposed to be a more natural way to represent a LINQ expression (although I personally prefer the extension methods). As has already been pointed out, the first example will return an IEnumerable result while the second example will return a List result due to the call to ToList(). If you remove the ToList() call in the second example, they will both return the same result as Where returns an IEnumerable result.</p>\n'}]
['https://Stackoverflow.com/questions/5194', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/439/']