Click here to Skip to main content
15,308,634 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I'm trying to put an if statement after the from clause in this query and check variable t:

C#
if(t == null)
   do nothing;
else
   select new TaskViewModel(t, this)).ToList<TaskViewModel>());
C/
vmSubTasks = new List<TaskViewModel>(
                (from t in vmTask.SubTasks
                 select new TaskViewModel(t, this)).ToList<TaskViewModel>());


Can I put a where clause? (where t != null)
I also want to check vmTask.SubTasks before use.
Posted
Updated 20-Oct-12 5:58am
v5
Comments
vasim sajad 20-Oct-12 9:34am
   
vmTask.SubTasks datatype ?

Here: Checking for possibly null values in LINQ[^]

Something like:
C#
var titleless = title == null ?
  items.Where(x => x.Title == null) : items.Where(x => x.Title == title);
   
Comments
cs101000 20-Oct-12 6:21am
   
I think this example is not my case, I improved the question.
vmSubTasks = vmTask.SubTasks == null 
    ? null 
    : (from t in vmTask.SubTasks
       where t != null
       select new TaskViewModel(t, this))
    .ToList<taskviewmodel>();</taskviewmodel>


or

vmSubTasks = vmTask.SubTasks == null 
    ? null 
    : vmTask.SubTasks
          .Where(t => t != null)
          .Select new TaskViewModel(t, this)
          .ToList<taskviewmodel>();</taskviewmodel>


Note that in your code snippet you are creating a list twice. The first is passed to the constructor of the second.
   

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