扩展方法和直接查询之间的区别

Kut*_*ith 4 .net c# linq

var selectedProducts = from p in products
                       where p.Category == 1
                       select p;

var selectedProducts =  products.Where(p=>p.Category==1) ;
Run Code Online (Sandbox Code Playgroud)

上述两个陈述似乎产生了相同的结果.

那有什么区别(有时是内部的)?

哪一个更有效率?

Kob*_*obi 6

没有区别.第一个(查询表达式)由编译器转换为第二个(并且对运行时没有影响).

也可以看看: