本文向大家介绍LINQ查询结果,可能好多人还不了解LINQ查询结果,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。
使用LINQ查询结果
如果查询结果是强类型的,如string[],List<T>等,就可以不用var类型,而是使用合适的 IEnumerable<T>或IEnumerable(因为IEnumerable<T>也扩展了IEnumerable)类型。
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("***** LINQ Transformations *****"n");
- IEnumerable<string> subset = GetStringSubset();
- foreach (string item in subset)
- {
- Console.WriteLine(item);
- }
- Console.ReadLine();
- }
- static IEnumerable<string> GetStringSubset()
- {
- string[] currentVideoGames = {"Morrowind", "BioShock",
- "Half Life 2: Episode 1", "The Darkness",
- "Daxter", "System Shock 2"};
- // Note subset is an IEnumerable<string> compatible object.
- IEnumerable<string> subset = from g in currentVideoGames
- where g.Length > 6
- orderby g
- select g;
- return subset;
- }
- }
使用LINQ查询结果的分页处理:
- var custTotalOrders = from c in db.D_WorkCenter
- //join o in db.Orders
- //on c.CustomerID equals o.CustomerID into custOrders
- //from o in custOrders
- select new
- {
- WorkCenterID = c.WorkCenterID,
- WorkCenterName = c.WorkCenterName
- //OrderTotal = o.Order_Details.Sum(d => d.UnitPrice * d.Quantity)
- }
【编辑推荐】