I am trying to ensure that only NOT null items are displayed in the Console.
My code is:
internal static void RenderViews(List<BusinessProcessFlow> businessProcessListRaw)
{
foreach (var results in businessProcessListRaw.Where(p => p.Index == 1))
{
var value = businessProcessListRaw.Where(x => results.EntityStep != null);
Console.WriteLine(value);
}
Console.ReadLine();
}
The result is an empty console screen.
You should first provide model definitions BusinessProcessFlowfor
class for a complete answer. If we assume that your model is something like this
public class BusinessProcessFlow
{
public int Id {get; set;}
public string Name { get; set; }
public int Index { get; set; }
public EntityStep EntityStep { get; set }
public override string ToString()
{
return $"{Id} {Name}";
}
}
And you want to filter all BusinessProcessFlow
with Index
equal to 1
and EntityStep
not null
, I think the query should be something like this
internal static void RenderViews(List<BusinessProcessFlow> businessProcessListRaw)
{
foreach (var result in businessProcessListRaw.Where(p => p.Index == 1 && p.EntityStep != null))
{
Console.WriteLine(result);
}
Console.ReadLine();
}
Also, the type of result
should have a ToString()
method override in order for the console to print the data in the format you want.
What you are currently doing is effectively iterating over the same list two times, but with different rules; the first time, you're getting each item where Index == 1
. The next time, you're getting items where EntityStep != null
. These two are not done together however, so you're likely to get a wrong answer.
You should be able to do all of that simpler using a slightly modified linq query:
internal static void RenderViews(List<BusinessProcessFlow> businessProcessListRaw)
{
var value = businessProcessListRaw
.Where(p => p.Index == 1 && p.EntityStep != null)
.ForEach(p => Console.WriteLine(p));
Console.ReadLine();
}
I'm afraid your main mistake is in this line:
var value = businessProcessListRaw.Where(x => results.EntityStep != null);
But if you say that nothing is output to the console, then most likely there is no such data in the passed collection.
You can see it here:
class Program
{
static void Main(string[] args)
{
var businessProcessListRaw = Init();
foreach (var results in businessProcessListRaw.Where(p => p.Index == 1))
{
var value = businessProcessListRaw.Where(x => results.EntityStep != null);
Console.WriteLine(value);
}
Console.ReadLine();
}
private static List<BusinessProcessFlow> Init()
{
return new List<BusinessProcessFlow>
{
new BusinessProcessFlow{Index = 1, EntityStep = new EntityStep()},
new BusinessProcessFlow{Index = 1, EntityStep = new EntityStep()},
new BusinessProcessFlow{Index = 2, EntityStep = new EntityStep()},
new BusinessProcessFlow{Index = 1},
new BusinessProcessFlow{Index = 1},
};
}
}
public class BusinessProcessFlow
{
public int Index { get; set; }
public EntityStep EntityStep { get; set; }
}
public class EntityStep
{
}
The output to the console (wrong one):
And yes, as you have already been answered, just change the condition in the Where
section
p => p.Index == 1 && p.EntityStep != null
Then your method will look like this:
internal static void RenderViews(List<BusinessProcessFlow> businessProcessListRaw)
{
foreach (var results in businessProcessListRaw.Where(p => p.Index == 1 && p.EntityStep != null))
{
Console.WriteLine(results);
}
Console.ReadLine();
}
Good luck!)
If your mission is
to ensure that only NOT null items are displayed in the Console.
then the following would do the job.
internal static void PrintNotNulls(List<BusinessProcessFlow> businessProcessListRaw)
{
foreach( var x in businessProcessListRaw)
{
if (x != null) Console.WriteLine(x);
}
}
If you wish to use LINQ (the .Where
bit) then the following should work.
internal static void PrintNotNulls(List<BusinessProcessFlow> businessProcessListRaw)
{
var notNulls = businessProcessListRaw
.Where( x => x != null);
foreach( var x in notNulls )
Console.WriteLine(x);
}
You can modify both approaches to check for more than just null: ' x.Index == 1
' or/and ' x.EntityStep != null
'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.