繁体   English   中英

如何使用LINQ将对象分为两组

[英]How to use LINQ to group objects in two groups

可以说我有发票类:

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的发票,并按价格的升序显示每个组中发票的详细信息,我该怎么做?

您可以简单地执行以下操作:

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方法更好。

如果使用组功能很麻烦(有时会很烦人),那么您也可以使用“哪里”

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();

您可以将范围的概念封装在一个类中:

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

您将获得一个IGrouping列表,每个IGrouping由指定的范围作为键,并附加了适合该范围的适当对象。

现在,显然上面的代码还不是生产级别,但是足以让您入门。 至于为什么它没有生产水平:

  • 没有检查可以断言提供给PriceRangeFactory的范围实际上确实完全覆盖了所有可能的值。
  • 始终假定范围描述为> X和<=Y。
  • 没有检查Range.Min <Range.Max。
  • 没有断言提供给PriceRangeFactory的范围截止列表是正确排序的(对于BinarySearch是必需的)。
  • 肯定会有一些我没有涉及的极端情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM