巫妖王
巫妖王

Reputation: 425

C# combined Find()

Is it possible to do something like below?

Interaction interaction=interactions.Find(i=>i.day==action.day,i=>i.scene==action.scene);

Upvotes: 0

Views: 68

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137438

I think what you're looking for is:

IEnumerable<Interaction> matchingInteractions = interactions.Where(
    i => (i.day==action.day && i.scene == action.scene)
);

This uses LINQ's Where which returns another IEnumerable of only the items that match the predicate function.

Alternatively, there is First, which returns the first item that matches the predicate.

Interaction firstInteraction = interactions.First(
    i => (i.day==action.day && i.scene == action.scene)
);

Finally, if you're certain there exists only one matching item, there is Single.

Upvotes: 2

Related Questions