Re: Linq Projection Question

Posts   
 
    
MarcoP avatar
MarcoP
User
Posts: 270
Joined: 29-Sep-2004
# Posted on: 16-May-2010 23:27:59   

I am still in the early stages of learning linq and i have a feeling the following projection can be done using the Select() extension method.

Are these two equivalent?


return MetaData.User.Select(Map).SingleOrDefault(dto => dto.Email == email); 

        public UserDTO GetByEmail(string email)
        {
            return Map(MetaData.User.SingleOrDefault(e => e.Email == email));
        }

        private UserDTO Map(UserEntity user)
        {
            if (user == null) return null;

            return new UserDTO(user.UserId, user.Email, user.FirstName, user.LastName, user.Avatar);
        }
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 17-May-2010 10:17:17   

You can do the following:


            var q = from c in metaData.Customers
                    select Map(c.CustomerId, c.CompanyName);

        private CustomerDTO Map(string customerId, string country)
        {
            return new CustomerDTO(customerId, country);
        }