C# Declare variable in lambda expression

后端 未结 3 596
轮回少年
轮回少年 2021-01-04 03:53

I want to do a simple lambda expression like this:

IList list = GetSomeList();

MyEntity1 result = list.SingleOrDefault(         


        
3条回答
  •  遥遥无期
    2021-01-04 04:34

    Well, first off, are you trying to use this in linq to sql / entity framework / other?

    If not, then just do this

    list.SingleOrDefault(e => {
       var entity = GetMyEntity2(e);
    
       return entity != null && entity.Id != null && entity.Id > 0;
    });
    

提交回复
热议问题