user6508956
user6508956

Reputation: 93

c# how to check if queue contains object property with a specific value?

I want to get Boolean value if queue contains object having particular value in the properties.

e.g)

public class Order
{
    public string orderType { get; set; }
    public string sCode { get; set; }  
    public int iNum { get; set; }
    ...omit... 
}
Queue<Order> queueSendOrder = new Queue<Order>();

Then, how to check if Queue contains or not if contains any object having sCode="Code1", iNum=1?

Thank you.

Upvotes: 2

Views: 3623

Answers (1)

DavidG
DavidG

Reputation: 118987

Using the Linq Any() extension method, this is quite simple:

var containsCode1 = queueSendOrder.Any(o => o.sCode == "Code1");
var containsNum1 = queueSendOrder.Any(o => o.iNum == 1);

Or both:

var containsCode1AndNum1 = queueSendOrder.Any(o => 
    o.sCode == "Code1"
    && o.iNum == 1);

Side note: It's considered bad practice these days to use Hungarian notation to denote types. So sCode should really just be Code and iNum would be Num (though I would choose a better name than that)

Upvotes: 5

Related Questions