我有以下两个POCO&#39>
public class Customers
{
public Guid Id { get; set; }
public string FirstName{ get; set; }
public string LastName{ get; set; }
}
和
public class Orders
{
public Guid Id { get; set; }
public Guid CustomerId { get; set; }
public string Product { get; set; }
public DateTime Date { get; set; }
}
假设我的数据服务返回一个List< Customers>和列表<订单>,我如何使用Linq获得最有序的产品?
我想返回Order.Id,Order.Product和Count(作为控制器动作的Json),所以我还需要创建一个新的POCO来保存结果。如下所示:
public class CustomerOrders
{
public Guid OrderId { get; set; }
public string Product { get; set; }
public int Count { get; set; }
}
答案 0 :(得分:1)
一种方法是执行以下操作:
@set num1=1
:repeat
@if %counter% == 500 exit
@set /a num1= %num1% + 1
@SET /A a=%RANDOM% * 10 / 32768 + 1
@if %a% == 10 set life%num%=----
@if %a% == 9 set life%num%=O
@if %a% == 8 set life%num%=^
@if %a% == 7 set life%num%=%
@if %a% == 6 set life%num%=#
@if %a% == 5 set life%num%=@
@if %a% == 4 set life%num%=)
@if %a% == 3 set life%num%=()
@if %a% == 2 set life%num%=~
@if %a% == 1 set life%num%=OO
@echo %a% >>"gencode".txt
@set /a num2= %num% + 1
@SET /A a=%RANDOM% * 10 / 32768 + 1
@if %a% == 10 set life%num%=----
@if %a% == 9 set life%num%=O
@if %a% == 8 set life%num%=^
@if %a% == 7 set life%num%=%
@if %a% == 6 set life%num%=#
@if %a% == 5 set life%num%=@
@if %a% == 4 set life%num%=)
@if %a% == 3 set life%num%=()
@if %a% == 2 set life%num%=~
@if %a% == 1 set life%num%=OO
@set final= 500
@set /a pass= %pass% + 1
@set /a cnum= %cnum% + 1
@set counterold=%counter%
@set life%cnum%-%counter%= %a%-%b%
@if %pass% == 1 goto start
:repeat
@if %ficnum% == 500 goto final
@set life2%cnum%-%counter%= life%counterold%-life%counter%
@set life2%cnum%-%counterold%= life%counterold%-life%counter%
@set /a ficnum= %ficnum% + 1
@set life3%ficnum%-%counter%= life2%counterold%-life2%counter%
@goto repeat
:final
@set /a final= %final% + 1
@set /a counter= %counter% + 1
@set /a ficnum= %ficnum% + 1
@set counter1= %counter%
@set ficnum1= %ficnum%
:finalst
@set /a final= %final% - 1
@set /a counter1= %counter1% - 1
@set /a ficnum= %ficnum% - 1
@set /a ficnum1= %ficnum1% - 1
@set /a counter= %counter% - 1
@set final%final%=%life3%%ficnum%-%counter%-%life3%%ficnum1%-%counter1%
@if %final% == 0 goto finalend
@goto finalst
:finalend
@echo final0>"lifeform".txt
List<Order> by Product
IEnumerable<CustomerOrders>
所以它看起来像这样:
Count
虽然(from o in orders
group o by o.Product into og
select new CustomerOrders { OrderId = og.First().Id,
Product = og.First().Product,
Count = og.Count() }
).OrderBy(r=>r.Count)
没有多大意义,除非它是OrderId
的列表,因为你想获得最有序的产品。
编辑:你是对的,这是正确的语法
答案 1 :(得分:0)
试试这个。您不需要客户列表。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Orders> orders = new List<Orders>();
List<CustomerOrders> customerOrders = new List<CustomerOrders>();
var productOrders = orders.AsEnumerable()
.GroupBy(x => x.Product).ToList();
foreach (var productOrder in productOrders)
{
CustomerOrders newOrder = new CustomerOrders();
customerOrders.Add(newOrder);
newOrder.OrderId = productOrder.FirstOrDefault().Id;
newOrder.Product = productOrder.FirstOrDefault().Product;
newOrder.Count = productOrder.Count();
}
}
}
public class Orders
{
public Guid Id { get; set; }
public Guid CustomerId { get; set; }
public string Product { get; set; }
public DateTime Date { get; set; }
}
public class CustomerOrders
{
public Guid OrderId { get; set; }
public string Product { get; set; }
public int Count { get; set; }
}
}
答案 2 :(得分:0)
如果您更喜欢使用c#语法而不是linq:
var groupedOrders = orders.GroupBy(o => o.Product);
这将返回每个组的IGrouping,其中key是产品名称,值是包含该产品的所有订单的IEnumerable
var sortedGroups = groupedOrders.OrderByDescending(g => g.Count());`
这会按照IEnumerable of orders
的计数对这些组进行排序var projectedOrders = sortedGroups.Select(x => new CustomerOrders(){OrderId = x.First().Id,
Product = x.First().Product,
Count = x.Count()});
此处提供示例in angular2 http module