下面的查询返回下面的示例数据。有好几天了。我希望能够展示这一天 每天都有相关数据。我的预期结果也可以在下面找到。不知怎的,我认为我需要 使用嵌套的foreach语句,但一直无法解决这个问题。我是Linq的新手。请帮忙 有任何想法。
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
这是我尝试过的一个例子,但没有用。
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;
}
}
}
答案 0 :(得分:0)
您接近ToLookup
方法。您应该能够使用这样的查询:
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();
}
或者,通常会想到的第一个选项是使用分组,与ToLookup
相比,分组提供延迟执行。这些查询中的任何一个都可以工作(并且您可以使用上面相同的foreach
循环):
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;
如果您对ToLookup
和GroupBy
之间的区别感到好奇,请查看this post。
请注意,按t.Day
排序是按字母顺序排列的,它是否真的符合您的目的?如果您的数据中有“星期五”,则会在“星期三”之前显示。如果你有日期,那么如果你真的需要订购结果,这会更好。