How to use the returned linq variable?

I decided to take a quick look at the LINQ side of things, and not just use the direct foreach loop, but I had problems with its operation, mainly because of the data types that I consider.

So I have it,

var selectedSiteType = from sites in siteTypeList where sites.SiteTypeID == temp select sites; 

siteTypeList is a list of SiteTypes. I am trying to find a specific one (which I denounced with the variable "temp".

How can I use this selected SiteType AS SiteType? When I try to pass "selectedSiteType" through another function, for example:

 mSiteTypeSub.EditSitetype(selectedSiteType); 

note: I tried to provide an index as if selectedSiteType was a list / array, but that didn't work either, I get the following error:

 Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<DeviceManager_take_2.SiteType>' to 'DeviceManager_take_2.SiteType' 

Am I missing something? perhaps some garbage? As if I said that I am new to this, and I am struggling to solve it. Most likely I have the whole concept wrong, and bingbangbosh I made myself a fool!

Greetings in advance.

+7
source share
3 answers

Use First / FirstOrDefault / Single / SingleOrDefault to get an element of a specific type from the collection.

  var value = selectedSiteType.First(); // returns the first item of the collection var value = selectedSiteType.FirstOrDefault(); // returns the first item of the collection or null if none exists var value = selectedSiteType.Single(); // returns the only one item of the collection, exception is thrown if more then one exists var value = selectedSiteType.SingleOrDefault(); // returns the only item from the collection or null, if none exists. If the collection contains more than one item, an exception is thrown. 
+17
source

If your return type is single:

  var selectedSiteType = (from sites in siteTypeList where sites.SiteTypeID == temp select sites).SingleOrDefault(); 

If the list (potentially more than one item):

  var selectedSiteType = (from sites in siteTypeList where sites.SiteTypeID == temp select sites).ToList(); 

This is SingleOrDefault / ToList, which you are missing in your request.

+7
source

Shane

I will not improve on previous answers. They were both correct. I will try to explain a little to you, so that you will understand this in the future a little better.

What happens when you write a piece of code, for example:

 var selectedSiteType = from sites in siteTypeList where sites.SiteTypeID == temp select sites; 

you do not put the answer in var (selectedSiteType), instead you create an expression tree that evaluates ONLY when you actually use it (in foreach or by calling one of the methods (for example. First () ,. ToList (), SingleOrDefault () etc.).

The default type of the from operator returned is IEnumerable <>, but if you call .First () or .SingleOrDefault () (etc.), you dive into that IEnumerable <> and get the specific element.

Hope this helps you better understand what is happening.

Lemme knows if I can add something or if I have something wrong.

Greetings

Max

+4
source

All Articles