在C#中,Lambda表達式和LINQ查詢經常結合使用,以簡化代碼并提高代碼的可讀性。以下是一些結合Lambda和LINQ查詢的技巧:
var query = list.Where(x => x.Age > 18);
var query = list.OrderBy(x => x.Name);
var query = list.Select(x => new { Name = x.Name, Age = x.Age });
var query = list.GroupBy(x => x.Department);
var query = list1.Join(list2, x => x.Id, y => y.Id, (x, y) => new { Name = x.Name, Age = y.Age });
通過結合Lambda表達式和LINQ查詢,可以輕松地實現各種查詢操作,提高代碼的可讀性和可維護性。