Mihai Labo
Mihai Labo

Reputation: 1082

Select object and modify field linq

I have the following Linq Query :

 var list = (from user in serviceu.Repository.FindFind(<<filtering nonsense>>)
                            from profile in serviceP.Repository.GetQuery().Where(p => p.userID == user.userID).DefaultIfEmpty()
                            select user ).ToList();

Basically i have to add ProfileId to my user entity. Doing this join only lets me select the user element but i want to change the value of user.profileID=profile.profileID ( I have extended the object so it has that field ). How can this be done the fastest way? I have solved it temporarily by doing this :

var list = (from user in serviceu.Repository.Find(<<filtering nonsense>>)
                                from profile in serviceP.Repository.GetQuery().Where(p => p.userID == user.userID).DefaultIfEmpty()
                                select new { user, profile.ProfileID }).ToList(); 

list.ForEach(el =>
                {
                    el.user.ProfileID = el.ProfileID.ToString();
                });

                return list.Select(x => x.user).ToList();


Is this the best ( fastest/elegant way )? Can it be done differently?

Upvotes: 1

Views: 213

Answers (1)

Habib
Habib

Reputation: 223207

Instead of ForEach and return try:

return list.Select(c => {c.user.ProfileID = c.ProfileID.ToString(); return c;}).ToList();

Upvotes: 2

Related Questions