Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use returned linq variable?

Tags:

c#

.net

linq

I've decided to take a quick look into the LINQ side of things, as opposed to just using a straight up foreach loop, but i'm having some trouble getting it to work, mainly due to datatypes i believe.

So i've got this, so far;

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

siteTypeList is a list of SiteTypes. I'm trying to find a particular one (Which i've denounced with variable "temp".

How do i then use this selected SiteType AS a SiteType? When i try and pass "selectedSiteType" through to another function, like so;

mSiteTypeSub.EditSitetype(selectedSiteType);

note: I tried with providing an index, as if selectedSiteType was a list / Array, but that didnt 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 a cast of some kind? Like i said i'm new to this and am struggling to get my head around this. Chances are i've got the whole concept wrong and bingbangbosh i've made a fool of myself!

Cheers in advance.

like image 886
Kestami Avatar asked Sep 18 '12 12:09

Kestami


3 Answers

Use First / FirstOrDefault / Single / SingleOrDefault to get an item of the particular 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. 
like image 65
Habib Avatar answered Oct 11 '22 16:10

Habib


If your return type is a single:

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

If a list (potentially more than one item):

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

It's the SingleOrDefault / ToList that you're missing from your query.

like image 7
Chris Dixon Avatar answered Oct 11 '22 16:10

Chris Dixon


Shane,

I'm not going to improve on the previous answers. They were both correct. I am going to try and explain a little bit to you, so that you understand it in the future a bit better.

What happens, when you write a piece of code like:

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

you don't put the answer into the var (selectedSiteType), instead, you are creating an expression tree, that is evaluated ONLY when you actually use it (in a foreach, or by calling one of the methods (like .First(), .ToList(), SingleOrDefault(), etc).

The default return type of a from statement, is an IEnumerable<>, but if you call the .First() or .SingleOrDefault() (etc), you will dive into that IEnumerable<> and get a specific item.

I hope this helps you better understand what is going on.

Lemme know if I can add anything or if I got anything wrong.

Cheers,

Max

like image 4
Max v. Steiger Avatar answered Oct 11 '22 16:10

Max v. Steiger