简体   繁体   English

使用linq更新嵌套列表的数据

[英]Updating data of a nested list with linq

I have a user object that contains a nested list and i need to change the value of a element in the 3rd list and return the user object. 我有一个包含嵌套列表的用户对象,我需要更改第三个列表中元素的值并返回该用户对象。

I want to do this with linq, below is the nested loop. 我想用linq做到这一点,下面是嵌套循环。

foreach (User itm in user)
{
    if (itm.destinations!=null)
    {
        foreach (Models.Api.destinations.Destination dm in itm.destinations)
        {
            if (dm.destinationData != null)
            {
                foreach (Models.Api.destinations.DestinationData destData in dm.destinationData)
                {
                    if (destData.type == "phone" & destData.data!="")
                    { 
                          //i want to update the destData.data here .. something like
                         destData.data ='updated data';
                    }
                }
            }
        }                
    }
}

I want the updated data to be available in the user object 我希望更新的数据在用户对象中可用

can someone help me achieve this with the LINQ 有人可以通过LINQ帮助我实现这一目标吗

Thanks in advance Tarak 在此先感谢Tarak

Try this: 尝试这个:

foreach (var x in
    (from itm in user
    where itm.destinations!=null
    from dm in itm.destinations
    where dm.destinationData != null
    from destData in dm.destinationData
    where destData.type == "phone" & destData.data != ""
    select new { itm, dm, destData }))
{
    /* put your update code here. */
}

You didn't give us what the update code should look like or even the object models for us to work from. 您没有给我们提供更新代码的外观,甚至没有给我们提供要使用的对象模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM