【问题标题】:Updating a list based on another list根据另一个列表更新列表
【发布时间】: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
  • 当然,如果您了解更多,请随时编辑您的问题。如果你这样做,它将再次被撞到首页。

标签: c# linq list


【解决方案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;
    } 
}

【讨论】:

  • 在这个目标上我得到“索引器的属性......无法分配给”。它是只读的。”
  • 我不这么认为,它有 { get;放; },这告诉我它不是。但显然我的知识有限。
猜你喜欢
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
相关资源
最近更新 更多