Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表对象中的项追加并返回到对象列表中的字符串数组中_C#_Asp.net Core - Fatal编程技术网

C# 将列表对象中的项追加并返回到对象列表中的字符串数组中

C# 将列表对象中的项追加并返回到对象列表中的字符串数组中,c#,asp.net-core,C#,Asp.net Core,我有如下模型: public class Rhistory { public User User { get; set; } } public class Vhistory { public User User { get; set; } } public class User { public string UserId { get; set; } } public class Info { public L

我有如下模型:

public class Rhistory
{      
     public User User { get; set; }    
}

public class Vhistory
{      
     public User User { get; set; }    
}

public class User
{
     public string UserId { get; set; }
}

public class Info
{
     public List<Rhistory> Rhistory { get; set; }
     public List<Vhistory> Vhistory { get; set; }
}
        string[] rHistoryUserIds = Info
                           .Where(u => !Equals(u.Rhistory, null))
                           .SelectMany(v => v.Rhistory)
                           .Select(c => new string[] { c.User.UserId })
                           .SelectMany(m => m)
                           .Distinct().Where(x => !string.IsNullOrEmpty(x)).ToArray();

        string[] vHistoryUserIds = Info
                           .Where(u => !Equals(u.Vhistory, null))
                           .SelectMany(v => v.Vhistory)
                           .Select(c => new string[] { c.User.UserId })
                           .SelectMany(m => m)
                           .Distinct().Where(x => !string.IsNullOrEmpty(x)).ToArray();

        string[] userIds = rHistoryUserIds.Union(vHistoryUserIds).ToArray();
有谁能给我建议一个更短更快的方法吗


提前谢谢

您可以直接将Rhistory和Vhistory中的userId子集选择到一个匿名类中,然后在select查询中使用union。最后,使用SelectMany获取userId数组

//将您的信息列表放入infos var-infos=GetInfos; var result=infos.Selectx=>new { RUserIds=x.rhistore.Selecta=>a?.User.UserId, Vuser.UserId=x.Vhistory.Selecta=>a?.User.UserId, }.Selectx=>new { UserIds=x.RUserIds.Unionx.VUserIds }.SelectManyx=>x.UserIds.Wherex=>x!=null.ToArray;
这就是我优化它的方式,感谢所有提出建议的人

        var rUsers = Info.Where(c => c.Rhistory != null)
                                 .SelectMany(cn => cn.Rhistory)
                                 .Select(users => users?.User?.UserId)
                                 .Where(u => !string.IsNullOrWhiteSpace(u))
                                 .Distinct();

        var vUsers = Info.Where(c => c.Vhistory != null)
                                 .SelectMany(cn => cn.Vhistory)
                                 .Select(users => users?.User?.UserId)
                                 .Where(u => !string.IsNullOrWhiteSpace(u))
                                 .Distinct();


        var userIds = rUsers.Union(vUsers).ToArray();

现有的代码有什么问题吗?太多选择太多选择太多。一个故事或Vhistory只有一个用户ID。一个简单的变量rhistoreusers=infosList.SelectManyx=>x.rhistore.Selecty=>y.User.UserId@viveknuna我只想缩短这个逻辑,就是这样。var vhistoryUserIds=infos.Whereitem=>item.Vhistory!=null.SelectManyitem=>item.Rhistory.Selectitem=>item.User.UserId.Distinct;var rhistoreuserids=infos.Whereitem=>item.rhistore!=null.SelectManyitem=>item.Vhistory.Selectitem=>item.User.UserId.Distinct;只是使用?当您选择历史用户时,运算符,并最终排除空值。