This article shows two keywords of LINQ. These keywords are very helpful when working with a set of object collections using LINQ.
First I will go through the Into keyword and its use.
I used the following collection in this article, on which I've done actions in this article.
- List<Mobile> objMobileCollection = new List<Mobile>
- {
- new Mobile{Provider="Apple",ModelName="IPhone3",ModelNumber="I100",MemoryCard=4},
- new Mobile{Provider="Apple",ModelName="IPhone4",ModelNumber="I101",MemoryCard=8},
- new Mobile{Provider="Apple",ModelName="IPhone4S",ModelNumber="I102",MemoryCard=16},
- new Mobile{Provider="Apple",ModelName="IPhone5",ModelNumber="I103",MemoryCard=32},
- new Mobile{Provider="Samsung",ModelName="GalaxyS",ModelNumber="S001",MemoryCard=4},
- new Mobile{Provider="Samsung",ModelName="GalaxyS2",ModelNumber="S002",MemoryCard=16},
- new Mobile{Provider="Samsung",ModelName="GalaxyS3",ModelNumber="S003",MemoryCard=20},
- new Mobile{Provider="Samsung",ModelName="Grand",ModelNumber="G001",MemoryCard=4},
- new Mobile{Provider="Nokia",ModelName="LUMIA530",ModelNumber="N1",MemoryCard=8},
- new Mobile{Provider="Nokia",ModelName="LUMIA730",ModelNumber="N2",MemoryCard=16},
- new Mobile{Provider="Nokia",ModelName="LUMIA930",ModelNumber="N3",MemoryCard=20},
- };
Into keyword
The Into keyword allows creating a temporary variable to store the results of a group, join or select clause into a new variable.
- var resultSet = from mobile in objMobileCollection
- group mobile by new { mobile.Provider } into mGroup
- orderby mGroup.Key.Provider
- select new
- {
- Provider = mGroup.Key.Provider,
- ModelName = mGroup.OrderBy(x => x.ModelName),
-
-
- };
In the above query after applying Into on mobile groping it creates the type mGroup variable to apply the next filter or create a temporary variable “mGroup” for further operations.
As in the code below:
- foreach (var items in resultSet)
- {
- Console.WriteLine("{0} - {1}", items.Provider, items.ModelName.Count());
- Console.WriteLine("------------------------------------------------");
- foreach (var item in items.ModelName)
- {
- Console.WriteLine(item.Provider + "\t" + item.ModelName + "\t\t" + item.ModelNumber.Trim() + "\t" + item.MemoryCard);
- }
- Console.WriteLine();
- }
- Console.ReadLine();
Please have a look at the output as shown in the following image:
Let Keyword
The Let keyword permits you to store the result of the query that can be used further in subsequent queries.
It creates a new variable and initializes it with the result of the expression and can be used further in queries just the opposite of the Into keyword, which means you created a new variable and you also can use the previous variable so you can use both in the further operations.
- var resultSetLet = (from mobile in objMobileCollection
- group mobile by new { mobile.Provider } into mGroup
-
- let avgMemory = mGroup.Sum(x => x.MemoryCard) / mGroup.Count()
- where avgMemory > 11
- select new
- {
- Provider = mGroup.GroupBy(x => x.Provider),
- ModelName = mGroup.OrderBy(m => m.ModelName),
- ModelNumber = mGroup.OrderBy(x => x.ModelNumber)
-
- }).ToList();
Kindly refer to the image shown below for further reference:
Please have a look at the complete code shown below:
- var resultSetLet = (from mobile in objMobileCollection
- group mobile by new { mobile.Provider } into mGroup
-
- let avgMemory = mGroup.Sum(x => x.MemoryCard) / mGroup.Count()
- where avgMemory > 11
- select new
- {
- Provider = mGroup.GroupBy(x => x.Provider),
- ModelName = mGroup.OrderBy(m => m.ModelName),
- ModelNumber = mGroup.OrderBy(x => x.ModelNumber)
-
- }).ToList();
-
- foreach (var items in resultSetLet)
- {
- Console.WriteLine("{0} - {1}", items.Provider, items.ModelNumber.Count());
- Console.WriteLine("------------------------------------------------");
- foreach (var item in items.ModelNumber)
- {
- Console.WriteLine(item.Provider + "\t" + item.ModelName + "\t\t" + item.ModelNumber + "\t" + item.MemoryCard);
- }
- Console.WriteLine();
- }
- Console.ReadLine();
Please have a look at the output as shown below in the image:
Note: Group by multiple columns in LINQ to SQL as shown in the image below:
I wish it will help you utilize both features at the best.
To learn more about MVC please go to the following link.
MVC ArticlesThanks.
Enjoy coding and reading.