Linq就是能够使开发人员能够像操作数据库那样操作内存。
var data = from p in lstCustomProfile orderby p.ModificationDate descending select new { p.CustomProfileid, p.Name };
CustomProfile model = lstCustomProfile.Where(p => p.FunctionCode.Equals("SaveQueryRecord")).OrderByDescending(p => p.ModificationDate).FirstOrDefault();
IList<CustomProfile> defaultCustoms = CustomProfileServiceAction.GetCustomProfiles(1)
.Where(C => C.FunctionCode.Equals("Default") && C.Satus.Equals("Released"))
.OrderBy(C => C.ProfileType).ToList();
3.Where限制
var q =
from p in db.Products
group p by p.CategoryID into g
where g.Count() >= 10
select new {
g.Key,
ProductCount = g.Count(p => p.Discontinued)
};
语句描述:根据产品的―ID分组,查询产品数量大于10的ID和产品数量。这个示例在Group By子句后使用Where子句查找所有至少有10种产品的类别。
DefaultIfEmpty 扩展方法
我们已经会用Empty生成空序列。 但问题是,如果你想返回一个序列当序列是空的时候包含一个默认的项目怎么办?
比如说,如果你正在分析测试成绩列表,如果学生没有得分,要返回一个 0:
var scores = new[] { 73, 77, 89, 90, 92, 77 };
foreach (var score in scores.DefaultIfEmpty())
{
Console.WriteLine("The score is: " + score);
}
List<int> emptyScores = new List<int>();
foreach (var score in emptyScores.DefaultIfEmpty())
{
Console.WriteLine("The score is: " + score);
}
现在,还有第二种形式的DefaultIfEmpty(),可让您指定要使用的默认值,而不是依赖于 Default(T) 。 例如,如果我们希望得到平均分,但如果是空值就要返回一个100.
var averageSoFar = scores.DefaultIfEmpty(100).Average();
Count
var scoreList = new List<int> { 73, 77, 89, 90, 92, 77 };
var numberOfBsOrBetter = scoreList.Count(s => s >= 80);
//为什么要用这个呢?既然我们有那个方便的重载
var numberOfBsOrBetter = scoreList.Where(s => s >= 80).Count();
参考资料:http://kb.cnblogs.com/page/42468/(李永京Linq To SQL)
http://developer.51cto.com/art/200911/165090.htm(Linq教程)