Creating a LINQ Expression where parameter equals object

前端 未结 3 988
野性不改
野性不改 2021-02-07 00:18

Given a primitive value age I know how to create an expression like this:

//assuming: age is an int or some other primitive type
employee => empl         


        
3条回答
  •  孤街浪徒
    2021-02-07 01:09

    You can't do that because EF doesn't know how to translate equality comparisons on Location into a SQL expression.

    However, if you know what properties of Location you want to compare, you can do this with anonymous types:

    var location = GetCurrentLocation();
    var locationObj = new { location.LocationName, location.LocationDescription };
    employees = DataContext.Employees.Where(e => new { e.Location.LocationName, e.Location.Description } == locationObj);
    

    Of course that's equivalent to:

    var location = GetCurrentLocation();
    employees = DataContext.Employees.Where(e => e.Location.LocationName == location.Name && 
                                                 e.Location.Description == location.Description);
    

提交回复
热议问题