Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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# Can';如果子列表为空,是否使用linq转换具有子列表的对象列表?_C#_List_Linq - Fatal编程技术网

C# Can';如果子列表为空,是否使用linq转换具有子列表的对象列表?

C# Can';如果子列表为空,是否使用linq转换具有子列表的对象列表?,c#,list,linq,C#,List,Linq,我有一段代码将一个对象列表转换为另一个对象列表。代码可以正常工作,但如果Messages属性为空,则无法正常工作。我以为使用Any with?:操作符可以修复它,但它并没有,相反,我得到了一个Null引用异常 var TemporaryMessageGroupList = MyList.Select(x => new MessageGroupModel() { Id = x.Id, Name = x.Name,

我有一段代码将一个对象列表转换为另一个对象列表。代码可以正常工作,但如果Messages属性为空,则无法正常工作。我以为使用Any with?:操作符可以修复它,但它并没有,相反,我得到了一个Null引用异常

var TemporaryMessageGroupList = MyList.Select(x => new MessageGroupModel()
        {
            Id = x.Id,
            Name = x.Name,
            DisplayPicture = x.DisplayPicture,
            LastMessage = x.LastMessage,
            Archived = x.Archived,
            Messages = (x.Messages.Any() ? x.Messages.Select(y => new MessageModel()
            {
                Id = y.Id,
                CreatorId = y.CreatorId,
                Content = y.Content,
                TimeSent = y.TimeSent,
                GroupId = y.GroupId,
                HasAttachment = y.HasAttachment,
                AttachmentImage = y.AttachmentImage,
                IsRead = y.IsRead
            }).ToList() : new List<MessageModel>()),
            Members = x.Members.Select(c => new UserModel()
            {
                Id = c.Id,
                FullName = c.FullName,
                DisplayPicture = c.DisplayPicture
            }).ToList()
        }).ToList();
var TemporaryMessageGroupList=MyList.Select(x=>newmessagegroupmodel()
{
Id=x.Id,
Name=x.Name,
DisplayPicture=x.DisplayPicture,
LastMessage=x.LastMessage,
已存档=x.已存档,
Messages=(x.Messages.Any()?x.Messages.Select(y=>newmessagemodel())
{
Id=y.Id,
CreatorId=y.CreatorId,
Content=y.Content,
TimeSent=y.TimeSent,
GroupId=y.GroupId,
HasAttachment=y.HasAttachment,
AttachmentImage=y.AttachmentImage,
IsRead=y.IsRead
}).ToList():新列表()),
Members=x.Members.Select(c=>newusermodel()
{
Id=c.Id,
FullName=c.FullName,
DisplayPicture=c.DisplayPicture
})托利斯先生()
}).ToList();

我只想能够将一个对象列表转换为另一个,如上图所示,如果有人知道另一种方法来克服此问题,请告诉我,谢谢

如果
消息
为空,则在
消息
上调用
任何
将产生上述异常,而应执行以下操作:

 Messages = x.Messages?.Select(y => new MessageModel()
            {
                Id = y.Id,
                CreatorId = y.CreatorId,
                Content = y.Content,
                TimeSent = y.TimeSent,
                GroupId = y.GroupId,
                HasAttachment = y.HasAttachment,
                AttachmentImage = y.AttachmentImage,
                IsRead = y.IsRead
            }).ToList() ?? new List<MessageModel>(),
Messages=x.Messages?。选择(y=>newmessagemodel()
{
Id=y.Id,
CreatorId=y.CreatorId,
Content=y.Content,
TimeSent=y.TimeSent,
GroupId=y.GroupId,
HasAttachment=y.HasAttachment,
AttachmentImage=y.AttachmentImage,
IsRead=y.IsRead
}).ToList()??新列表(),
  • 这将使用“空传播”操作符
    ?。
    防止调用
    消息上的
    选择
    ,如果它为空
  • 然后,在
    消息
    为空的情况下,我们使用“null coalescing”操作符
    生成一个新的空列表

您可以尝试类似的方法,确保x.Messages不为NULL,如果为空,则首选为空集合,还可以确保原始对象的消息属性不为NULL,或者接收对象的消息属性为NULL:

Messages = (x.Messages?.Where(msg => msg != null).Select(y => new 
MessageModel() {}).ToList()

x.Messages!=空