0

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

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
sara adly
  • 297
  • 1
  • 5
  • 19

1 Answers1

2

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?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Error 114 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) – sara adly Feb 29 '16 at 14:03
  • @saraadly: you need to append `ToList()`. However, it's not clear how you want to convert this list into a `string` that is returned from the method. – Tim Schmelter Feb 29 '16 at 14:05