0

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.

2 Answers 2

1

This should do what you want:

byte minTerm = yourProducts.SelectMany(x => x.Terms).Min(x => x.MinTerm);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.