How do i use an Expression<Func<T>> in a linq statement if my navigation property is a single item?

Viewed 133

I have an expression that maps the database object from another system to a standard object that I can use across any system.

Ex:

private static Expression<Func<excessive_location, MyLocation>> LocationMap =
    l => new MyLocation()
        {
            Id = l.unique_id,
            Code = l.location_code,
            Active = l.active

            ...Other properties
        }

This works exactly as I expect in the following situation

public IEnumerable<MyLocation> GetActiveLocations()
        {
            return otherSystem.location_table
                .Select(LocationMap)
                .Where(l => l.Active == true)
                .ToList();
        }

But I cant seem to figue out a way to make it work as part of another expression

private static Expression<Func<excessive_user, User>> UserMap =
    e => new User()
        {
            Id = e.unique_id,
            FirstName = e.fname,
            LastName = e.lname,
            Location = e.excessive_location
                .Select(LocationMap)  // will not work since
                                      // e.excessive_location is not a collection
        };

I know I can compile it to a function but then it will have to execute for every user out of thousands. What is the proper way to make this work?

2 Answers
Related