Get minimum value in linq query

I'm using Entity Framework for my DB access.

One of the entities is Products, and a Product can have many Terms.

Here's the Term class:

public partial class Term
{
    public short ID { get; set; }
    public short ProductID { get; set; }
    public byte TermSegmentID { get; set; }
    public byte MinTerm { get; set; }
    public byte MaxTerm { get; set; }


    public virtual Product Product { get; set; }
}

Having selected a list of all my products, I've been trying to return the minimum value in MinTerm - that's the lowest value for all Products, not each one.

Can anyone help? This is proving difficult for my limited knowledge.

Thanks in advance.

Jon Skeet
people
quotationmark

Does Product have a Terms property? If so, it's pretty easy:

var minMinTerm = products.SelectMany(product => product.Terms)
                         .Min(term => term.MinTerm);

The SelectMany method "flattens" a sequence - so you end up with a single sequence of terms, logically the concatenation of each of the terms sequences from the products.

people

See more on this question at Stackoverflow