Entity Framework - select

I have a table called Tag with a Label column and an AuctionId column. I also have an array of strings that are search terms. I want to write some Linq to Entities code that will give me a great list of AuctionIds, where the Label matches one of the search conditions. Here is the pseudo code for this:

return a list of unique AuctionIds where Label is in searchTerms 

How can I do that?

+7
source share
2 answers

You can use Contains () in the list.

 List<String> AuctionIDs = (from tagItem in Tags where searchItems.Contains(tagItem.Label) select tagItem.AutionID).Distinct().ToList(); 
+15
source

Using the Lambda notation for clarity, it breaks down into several functions in sequence as follows:

 IEnumerable<Int32> DistinctIds = TagTable.Where(x => searchTerms.Contains(x.Label)).Select( x => x.AuctionId).Distinct() 

Without going too far into lambda syntax, the main functions are here:

.Where (x => searchTerms.Contains (x.Label)) - this will only select rows in which the searchTerms collection contains the Label value for this row

.Select (x => x.AuctionId) - return only the integer values โ€‹โ€‹AutionId, not the full record

.Distinct () - does what it says on the prong

Hope this helps

+5
source

All Articles