假设我有班级发票:
public class Invoice
{
public int PartNumber { get; set; }
public string PartDescription { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}
然后我在变量 arrayOfInvoices 中有一个对象数组。
如果我必须将发票分为两组 - 单价低于12的发票和单价高于或等于12的发票,并按价格的升序显示每组发票的详细信息,我该怎么办? ?
答案 0 :(得分:2)
您可以简单地执行以下操作:
var results =
from inv in arrayOfInvoices
orderby inv.Price
group inv by inv.Price < 12;
或者如果您更喜欢流利的语法:
var results = arrayOfInvoices.OrderBy(inv => inv.Price)
.GroupBy(inv => inv.Price < 12);
要将发票分组为三个或更多“存储桶”,您可以使用BinarySearch
:
var priceBoundaries = new[] { 12m, 20m };
var results =
from inv in arrayOfInvoices
orderby inv.Price
let index = Array.BinarySearch(priceBoundaries, inv.Price)
group inv by (index < 0 ? ~index : index + 1);
或使用副作用,如下:
var priceBoundaries = new[] { 12m, 20m, Decimal.MaxValue }; // Note the addition of MaxValue
var i = 0;
var results =
from inv in arrayOfInvoices
orderby inv.Price
group inv by (inv.Price < priceBoundaries[i] ? i : ++i);
这通常是不好的做法,但应该比上面的BinarySearch方法表现更好。
答案 1 :(得分:2)
如果使用群组功能很痛苦(有时会让人讨厌),那么你也可以使用“Where”
var invoices = new List<Invoice> ();
var group1= invoices.Where(i=> i.Price<12).Orderby(i=> i.Price).ToList();
var group2= invoices.Where(i=> i.Price>=12).Orderby(i=> i.Price).ToList();
答案 2 :(得分:0)
您可以在类中封装范围的概念:
private class PriceRange<T> : IEquatable<PriceRange<T>>
{
public T Min { get; set; }
public T Max { get; set; }
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 31 + Min.GetHashCode();
hash = hash * 31 + Max.GetHashCode();
return hash;
}
}
public override bool Equals(object obj)
{
return Equals(obj as PriceRange<T>);
}
public bool Equals(PriceRange<T> other)
{
if (other == null) return false;
if (!Min.Equals(other.Min)) return false;
if (!Max.Equals(other.Max)) return false;
return true;
}
}
然后使用地图功能将每张发票的价格映射到适当的范围:
private class PriceRangeFactory<T>
{
public PriceRangeFactory(T[] rangeCutoffs)
{
_RangeCutoffs = rangeCutoffs;
}
private T[] _RangeCutoffs;
public PriceRange<T> Map(T price)
{
var index = Array.BinarySearch(_RangeCutoffs, price);
// Assume that the _RangeCutoffs that we have fully cover all possible values for T.
if (index < 0) index = ~index;
return new PriceRange<T>() { Min = _RangeCutoffs[index - 1], Max = _RangeCutoffs[index] };
}
}
然后将GroupBy与该map函数一起使用:
var rangeFactory = new PriceRangeFactory<decimal>(new decimal[] { decimal.MinValue, 12, 20, decimal.MaxValue });
var grouped = foos.GroupBy(a => rangeFactory.Map(a.Price));
您将获得一个IGroupings列表,每个IGroupings都按您指定的范围键入,并附带适合该范围的相应对象。
现在,显然上面的代码不是很高的生产水平,但它应该足以让你入门。至于为什么它不是生产水平: