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;
}
}
}