1

我有以下 Linq 查询:

 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();

基本上我必须将 ProfileId 添加到我的用户实体中。执行此连接仅允许我选择用户元素,但我想更改的值user.profileID=profile.profileID(我已经扩展了对象,因此它具有该字段)。如何以最快的方式做到这一点?我已经通过这样做暂时解决了它:

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();


这是最好的(最快/优雅的方式)吗?可以做不同的事情吗?

4

1 回答 1

2

而不是 ForEach 并返回尝试:

return list.Select(c => {c.user.ProfileID = c.ProfileID.ToString(); return c;}).ToList();
于 2012-04-12T18:58:06.070 回答