Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to write the code below in LINQ

Thank you,

List<Type> selectedList = new List<Type>();
foreach (Type type in allTypes)
{
  if (type != null)
  {
      if (type.BaseType != null)
      {
         if (type.BaseType.Name == "MyBASETYPE")
         {
            inheritedList.Add(type);
         }
      }
   }
}


What I have tried:

I wrote this but it seems it's not working because for some case, the baseType is null

var selectedList = allTypes.Where(p => p.BaseType.Name == "MyBASETYPE").Select(p => new { p });
Posted
Updated 9-Aug-18 22:37pm
Comments
Graeme_Grant 9-Aug-18 18:59pm    
Remove .Select(...).

Not certain what you expect from inheritedList in your example. Other than that, I believe you are looking for the following (to be fully equivalent):

var selectedList = allTypes
  .Where(type => type != null && type.BaseType != null &&
    type.BaseType.Name == "MyBASETYPE")
  .ToList();
 
Share this answer
 
Comments
Graeme_Grant 9-Aug-18 23:38pm    
.ToList() is only required if early evaluation is required. Also, you can simplify your answer to:
var selectedList = allTypes
  .Where(type => type?.BaseType?.Name == "MyBASETYPE");
Mycroft Holmes 10-Aug-18 2:27am    
And yet again I learn something - you have no idea how much crap I have gone through trapping nulls!
Graeme_Grant 10-Aug-18 2:36am    
It is definitely a time-saving refactoring! :)
Eric Lynch 10-Aug-18 8:35am    
Correct on both counts...and worth pointing out to other readers.

Regarding my solution, I am aware of the newer operators. I considered using them in my solution, but decided against it. I wanted to teach him how to convert his loop, without troubling him with having to also learn the newer language features. The code provided was as much like his original as I could make it, to illustrate the differences between using foreach and LINQ.

I am also aware of the perils of premature materialization. However, in his example, selectedList was specifically declared as a list, which is why I included ToList. For other readers, for more information on the perils of premature materialization, please check out my earlier article:

https://www.codeproject.com/Articles/1236958/LINQ-Part-A-Deep-Dive-into-IEnumerable

The reason I wrote the article was to better explain IEnumerable and hopefully help people avoid the mistake of always tacking on ToList...or anything else that would trigger premature materialization.

On an unrelated note, I was surprised to see a two star rating on the solution. Not sure what I did to deserve that :)
George Swan 10-Aug-18 12:02pm    
A great reply. Not that it makes much difference but isn't your solution an OR operation?
Rather than trying to use LINQ for no reason (if you think it is going to perform better think again, LINQ is often slower), you'd be better improving your understanding of the basics.

List<Type> selectedList = new List<Type>();
foreach (Type type in allTypes)
{
  if (type != null && type.BaseType != null && type.BaseType.Name == "MyBASETYPE")
  {
      inheritedList.Add(type);
   }
}


As mentioned in the other solution depending on your version of .net you can also use "?."
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900