sara adly
sara adly

Reputation: 297

Check two null and eqauilty In LinQ

I am not strong in LinQ So I try to check if two property is not null and one of them is equal variable number

so

public static string FilldrpRequestExecutionUnitsEmployee(int unitID)
{
    List<ApplicationStep> myAppList = 
        new ApplicationStepLogic(ApplicationType.Web)
        .GetAll()
        .Where(x => x.UnitId ==(int?) unitID && (x.UnitId && x.variable != null));
    return "";
}

please help I am linq beginner

Upvotes: 0

Views: 63

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460068

This should do it:

....
.Where(x => x.UnitId == (int?)unitID && x.variable != null)
.ToList();

You don't need the additional null check on the UnitId nullable if you already compare it with the casted unitID-int. Nullable<T>.Equals is overridden meaningfully and is safe(no exception if it's null). Fyi:Why does the == operator work for Nullable when == is not defined?

Upvotes: 2

Related Questions