使用LINQ从List 中提取结果

时间:2021-06-25 21:51:50

I have a List of Customers

我有一个客户列表

List<Customers> cust = new List<Customers>();
cust.Add(new Customers(){ID=1, Name="Sam", PurchaseDate=DateTime.Parse("01/12/2008")});
cust.Add(new Customers(){ID=2, Name="Lolly" PurchaseDate=DateTime.Parse("03/18/2008")});

I want to show 2 seperate results like:

我想显示2个单独的结果,如:

Purchases by Customer in Yr // Grouping by yr and display id,name
Purchases by Customer in Yr - Month // Grouping by yr then month and display id,name

客户在Yr中购买//按年份分组并显示ID,名称按客户购买年份 - 月份//按月份分组然后显示月份和显示ID,名称

Also What if i want to order the yr?

如果我想订购年,该怎么办?

Update:

Just one more addition. If I have a field called "Status" in the Customer class with either of these values 'Y', 'N', 'C' standing for yes, no and cancel how will i create a query to give ratio in %

再补充一点。如果我在Customer类中有一个名为“Status”的字段,其中任何一个值为'Y','N','C'代表yes,no和取消我将如何创建一个查询以给出%的比率

Y - 20% N - 30% C - 50%

Y - 20%N - 30%C - 50%

2 个解决方案

#1


Grouping by year:

按年分组:

var groupByYear = customers.GroupBy(customer => customer.PurchaseDate.Year);

foreach (var group in groupByYear)
{
    Console.WriteLine("Year: {0}", group.Key);
    foreach (var customer in group)
    {
        Console.WriteLine("{0}: {1}", customer.ID, customer.Name);
    }
}

Grouping by year and month:

按年和月分组:

var groupByYearMonth = customers.GroupBy(customer => 
     new DateTime(customer.PurchaseDate.Year, customer.PurchaseDate.Month, 1));
foreach (var group in groupByYear)
{
    Console.WriteLine("Year/month: {0}/{1}", group.Key.Year, group.Key.Month);
    foreach (var customer in group)
    {
        Console.WriteLine("{0}: {1}", customer.ID, customer.Name);
    }
}

Ordering:

var ordered = customers.OrderBy(customer => customer.PurchaseDate.Year);

All of these use "dot notation" instead of query expressions because they're so simple - but you could use a query expression if you wanted to.

所有这些都使用“点符号”而不是查询表达式,因为它们非常简单 - 但如果您愿意,可以使用查询表达式。

EDIT: For the status part, just use David B's answer.

编辑:对于状态部分,只需使用David B的答案。

#2


int total = customer.Count()

var counts = customers.GroupBy( c => c.Status )
  .Select( g => new
  {
    Status = g.Key,
    TheRatio = (g.Count() * 100) / total;
  })

#1


Grouping by year:

按年分组:

var groupByYear = customers.GroupBy(customer => customer.PurchaseDate.Year);

foreach (var group in groupByYear)
{
    Console.WriteLine("Year: {0}", group.Key);
    foreach (var customer in group)
    {
        Console.WriteLine("{0}: {1}", customer.ID, customer.Name);
    }
}

Grouping by year and month:

按年和月分组:

var groupByYearMonth = customers.GroupBy(customer => 
     new DateTime(customer.PurchaseDate.Year, customer.PurchaseDate.Month, 1));
foreach (var group in groupByYear)
{
    Console.WriteLine("Year/month: {0}/{1}", group.Key.Year, group.Key.Month);
    foreach (var customer in group)
    {
        Console.WriteLine("{0}: {1}", customer.ID, customer.Name);
    }
}

Ordering:

var ordered = customers.OrderBy(customer => customer.PurchaseDate.Year);

All of these use "dot notation" instead of query expressions because they're so simple - but you could use a query expression if you wanted to.

所有这些都使用“点符号”而不是查询表达式,因为它们非常简单 - 但如果您愿意,可以使用查询表达式。

EDIT: For the status part, just use David B's answer.

编辑:对于状态部分,只需使用David B的答案。

#2


int total = customer.Count()

var counts = customers.GroupBy( c => c.Status )
  .Select( g => new
  {
    Status = g.Key,
    TheRatio = (g.Count() * 100) / total;
  })