-1

Simple version

var myVar = some object;

myList.Where(element => myVar != null && element.Name == myVar.Name)

What I want to write

public static (Return Value) Start<T>(this T input, Func<(IDK what goes here)> exp)
{
   var theVar = somehow get the myVar that was passed in;
   if (theVar != null)
   {
       apply exp; <= I can do this part
   }
}

myList.Start((myVar, element) => element.Name == myVar.Name)); 

Basically be able to write an expression without having to check for the null every single time.

3 Answers3

0

Use anonymous lambda (I think without testing) >

myList.Start((myVar, () => element) => element.Name == myVar.Name)); 
RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
0

I found a way, i wrote a class BaseExtension it doesn't have a lot in it, just a holder for some properties

public static BaseExtension<T> Start<T, TValue>(this IQueryable<T> input, TValue value, Func<TValue, Expression<Func<T, bool>>> expression) =>
        new BaseExtension<T>(input, value == null ? null : expression.Invoke(value));

this allows me to write

_context.MyEntities.Start(myVar, value => entity => entity.Id.ToString() == myVar)

and passing in myVar allows me to do c# logic behind the scenes before the query would be executed in the database.

An example would be myVar != null or I can add a custom function that would apply the expression or not

-1

The where method expected a Boolean type, you can use this

  private static <type> <name>(<type> arg)
        {
            return true;
        }

If this not work for you, on Visual Studio, writing something insidere where, and press ctrl + . .

See the sugested implementations

Juliano Braz
  • 41
  • 1
  • 1
  • 5