neeraj
neeraj

Reputation: 345

how to select an item from generic list by linq

I have a LINQ query which contains a method GetInstanceForDatabase()

principlesList.Select(p => p.GetInstanceForDatabase()).ToList()

where

List<PrincipleInstance>() principlesList = ...
// (contains list of principle like "Manual Trades", "OPM", "Flora")

GetInstanceForDatabase() is a method which takes all other info about a principle (like manual trades).

My problem is that I want to sort out only principle like only "Manual Trades".

I want to put a where clause. I tried but it is fails.

Upvotes: 1

Views: 12143

Answers (3)

Jonny Cundall
Jonny Cundall

Reputation: 2612

To get a single item use:

query.First(x => x.property == "Manual Trades");
// or
query.FirstOrDefault(x => x.property == "Manual Trades");

Upvotes: 2

Haris Hasan
Haris Hasan

Reputation: 30097

This is the correct syntax of using Where in LINQ

principlesList.Select(p => p.GetInstanceForDatabase()).Where(p => p.SomeProperty == "SomeValue").ToList();

Upvotes: 0

FiveTools
FiveTools

Reputation: 6030

var list = p.GetInstanceForDatabase().where(x => x.propertyName == "Manual Trades").ToList();

I'm sure you're GetInstanceForDatabase needs to return your collection that you then filter for the 'Manual Trades' but I can't really tell how you get your list of PrincipalInstances from the question.

Upvotes: 0

Related Questions