The query below returns the sample data below. There are multiple days. I want to be able to display the day with the associated data under each day. My expected result can also be found below. Somehow I think I need to use a nested foreach statement but have been unable to figure this out. I am new to Linq. Please assist if you have any idea.
var pracResult = from t in queryResultFilter
orderby t.Day
select t;
Int32 okeke = 0;
foreach (MyPlanDto chk in pracResult)
{
okeke = chk.Day;
}
Sample data
*************
Day = Monday
CustomerNumber = 1001
Product = Dress
Day = Tuesday
customerNumber = 1002
Product = Boxers
Day = Wednesday
CustomerNumber = 1003
Product = Pencil
Day = Monday
CustomerNumber = 1006
Product = Pen
Day = Monday
CustomerNumber = 1007
Product = Book
Day = Tuesday
CustomerNumber = 1008
Product = Erazer
Result:
********
Monday
********
CustomerNumber = 1001
Product = Dress
CustomerNumber = 1006
Product = Pen
CustomerNumber = 1007
Product = Book
Tuesday
********
customerNumber = 1002
Product = Boxers
CustomerNumber = 1008
Product = Erazer
Wednesday
***********
CustomerNumber = 1003
Product = Pencil
Here is an example I tried but not working.
using System;
using System.Collections.Generic;
using System.Linq;
namespace MasterData
{
class Program
{
static void Main(string[] args)
{
var queryResultFilter = ObjMyPlan();
var pracResult = (from t in queryResultFilter
orderby t.Day
select t).ToLookup(p => p.Day).Select(col1 => col1.First());
var pracResult2 = (from t in queryResultFilter
orderby t.Day
select t);
foreach (MyPlanDto chk in pracResult)
{
Console.WriteLine(chk.Day);
foreach (MyPlanDto obj1 in pracResult2)
{
var yoyou = (from t in pracResult2
select t).Where(p=>p.Day.Equals(chk.Day));
Console.WriteLine("Customer Number:" + obj1.CustomerNumber + "Product:" + obj1.Product);
}
}
Console.ReadKey();
}
static List<MyPlanDto> ObjMyPlan()
{
List<MyPlanDto> objResult = new List<MyPlanDto>();
objResult.Add(new MyPlanDto(){Day = "Monday", CustomerNumber = "1001", Product = "Dress"});
objResult.Add(new MyPlanDto(){Day = "Tuesday", CustomerNumber = "1002", Product = "Boxers"});
objResult.Add(new MyPlanDto(){Day = "Wednesday", CustomerNumber = "1003", Product = "Pencil"});
objResult.Add(new MyPlanDto(){Day = "Monday", CustomerNumber = "1006", Product = "Pen"});
objResult.Add(new MyPlanDto(){Day = "Monday", CustomerNumber = "1007", Product = "Book"});
objResult.Add(new MyPlanDto(){Day = "Tuesday", CustomerNumber = "1008", Product = "Erazer"});
return objResult;
}
}
}
You're close with the ToLookup
approach. You should be able to use a query like this:
var query = queryResultFilter.OrderBy(t => t.Day)
.ToLookup(t => t.Day);
foreach (var group in query) {
Console.WriteLine(group.Key);
foreach (var item in group) {
Console.WriteLine("CustomerNumber: {0}", item.CustomerNumber);
Console.WriteLine("Product: {0}", item.Product);
}
Console.WriteLine();
}
Alternately, typically the first option that comes to mind is to use grouping, which offers deferred execution compared to ToLookup
. Either of these queries would work (and you can use the same foreach
loop above):
var query = queryResultFilter.OrderBy(t => t.Day)
.GroupBy(t => t.Day);
// query syntax
var query = from t in queryResultFilter
orderby t.Day
group t by t.Day into grouped
select grouped;
If you're curious about the difference between ToLookup
and GroupBy
, check out this post .
Note that ordering by t.Day
is alphabetical, so does it really serve the purpose you intend? If you have "Friday" in your data it will show up before "Wednesday" when ordered. If you have dates, that would work better if you really need to order the results.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.