How to use returned linq variable?

前端 未结 3 814
南笙
南笙 2021-01-18 05:00

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

3条回答
  •  囚心锁ツ
    2021-01-18 05:46

    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. 
    

提交回复
热议问题