【发布时间】:2017-07-24 23:30:50
【问题描述】:
我有两个用户列表。
首先,用户有以下字段 - fname,lname, UserDetailsId,FocusStart,FocusEnd,isActive
在第二个列表中,用户拥有 - fname、lname、UserDetailsId、totalTime、FocusStart、FocusEnd。
我的目标是:当第一个列表中的 isActive 值等于“true”并且 userDetailsId 等于第二个列表中的 UserDetailsId 时,我希望第二个列表中的 FocusStart 和 FocusEnd 等于第一个列表中的匹配元素。
关于如何实现这一点的任何提示?
这是我获得第一个列表的方法:
var list = listWRUD.
Join(db.UsersDetails,
o => o.UserDetailsId, od => od.identtyUserId,
(o, od) => new
{
fname = od.FirstName,
lname = od.LastName,
UserDetailsId = o.UserDetailsId,
FocusStart = o.FocusStart,
FocusEnd = o.FocusEnd,
isActive = o.isActive
}).ToList();
var a = from x in list
group x by new { x.fname, x.lname, x.UserDetailsId } into g
select new RolesUsersViewModel(g.Key.UserDetailsId, g.Key.fname, g.Key.lname, TimeSpan.FromMilliseconds(g.Sum(x => (x.FocusEnd - x.FocusStart).TotalMilliseconds)));
这是第二个:
List<RolesUsersViewModel> list_users = a.ToList<RolesUsersViewModel>();
到目前为止我得到的是:
var allActive = list.Where(item => item.isActive == true);
foreach (var p in list_users.Join(allActive, item => item.userId, item => item.UserDetailsId, (x, y) => new { L2 = x, L1 = y }))
{
p.L2.FocusStart = p.L1.FocusStart;
p.L2.FocusEnd = p.L1.FocusEnd;
}
遗憾的是,这段代码似乎给了我一些随机结果。即使第一个列表中没有 isActive==true 的记录,也会为第二个列表中的记录设置日期。
视图模型:
公共类 RolesUsersViewModel { 公共 RolesUsersViewModel(字符串 userDetailsId,字符串 FirstName,字符串 LastName,TimeSpan totalex) {
userId = userDetailsId;
fname = FirstName;
lname = LastName;
total = totalex;
}
public RolesUsersViewModel(DateTime focusStart, DateTime focusEnd)//
{
FocusStart = focusStart;
FocusEnd = focusEnd;
}
public string userId { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public TimeSpan total { get; set; }
public DateTime FocusStart { get; set; }//
public DateTime FocusEnd { get; set; }//
}
【问题讨论】:
-
I want the FocusStart and FocusEnd in the second list to be equals to the values of the matched element in the first list.是什么意思?是否要分配 FocusStart 和 FocustEnd? -
@CodingYoshi 是的,没错。
-
join中
item.userId从哪里来? -
第二个列表,@GertArnold
-
当然,如果您了解更多,请随时编辑您的问题。如果你这样做,它将再次被撞到首页。