2017-03-05 146 views
1

我有两个用户列表。基于另一个列表更新列表

在第一个用户有以下字段 - 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,串名字,串名字,时间跨度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; }// 

}

+0

什么做你的意思是'我希望FocusStart和FocusEnd在第二个列表中等于matc的值hed元素在第一个列表中。你想分配FocusStart和FocustEnd吗? – CodingYoshi

+0

@CodingYoshi是的,这是正确的。 –

+0

'item.userId'来自哪里? –

回答

1
foreach (var p in list_users) 
{ 
    // Get all the items that have matching UserDetailsId 
    var targets = allActive.Where(x => x.UserDetailsId == p.UserDetailsId); 

    // Now assign the properties 
    // my assumption is that the above query should return 
    // a single record. If my assumption is true then use 
    // Single or SingleOrDefault and then you do not need 
    // the loop below but just a simple assignment 
    foreach(var thisTarget in targets) 
    { 
     p.FocusStart = thisTarget.FocusStart; 
     p.Focused = thisTarget.FocusEnd; 
    } 
} 
+0

关于thisTarget我得到了“索引器的属性...不能被分配给”。 “ –

+0

@robertross是属性只读? – CodingYoshi

+0

我不这么认为,它有{get; set;},它告诉我它不是。但显然我的知识是有限的。 –