If condition check in linq

Below are the linq query. Here I want to add one condition . Condition : If : Firstname is not empty then select list where (d=>d.firstname=="Firstname") else: select all list without condition

function Ponits(string Firstname)
{

     pointsCore.Categories.SelectMany(c => c.Events).Select(e => new
     {
            e.Firstname,
            e.Surname,
            e.EntityNumber,
            e.Eventdate
     }).ToList()
}
Jon Skeet
people
quotationmark

Two options:

First, optionally use Where:

var events = pointsCore.Categories.SelectMany(c => c.Events);
if (!string.IsNullOrEmpty(firstName))
{
    events = events.Where(e => e.Firstname == firstName);
}
var result = events.Select(e => new { ... });

Second: make your Where clause check firstName:

var events = pointsCore.Categories.SelectMany(c => c.Events);
                       .Where(e => string.IsNullOrEmpty(firstName) ||
                                   e.Firstname == firstName)
                       .Select(e => new { ... });

Note that due to the lazy evaluation in LINQ, the first option won't involve fetching all the values and then querying; you're still just building up a query.

people

See more on this question at Stackoverflow