简体   繁体   English

如何返回 IEnumerable<t> 作为 null</t>

[英]How to return IEnumerable<T> as null

I am having the below ActivitiesDB.cs which is a DBEntity class.我有以下ActivitiesDB.cs ,它是一个DBEntity class。

public class ActivitiesDB
{
public IEnumerable<Activity> Activities { get; set; }
}

public class Activity
{
public Guid ActivityId { get ;set; }
public DateTime ActivityDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
public IEnumerable<MetaData> MetaData { get; set; }
}

public class MetaData
{
public string Name { get; set; }
public string Value { get; set; }
}

sample data in DB(I am using CosmosDb in backend): DB 中的示例数据(我在后端使用 CosmosDb):

"Activities": [
            {
                "ActivityId": "4f436d11-7a61-4e7f-b0fb-0f0d5bb6be76",
                "ActivityDateTime": "2021-04-30T09:35:35.32Z",
                "UpdatedDateTime": "2021-05-03T07:45:21.5115588Z",
                "Metadata": [
                    {
                        "Name": "name",
                        "Value": "value"
                    }
                ]
            },
            {
                "ActivityId": "659e62c5-4408-42b4-8719-b8c577221d22",
                "ActivityDateTime": "2021-04-30T09:35:35.32Z",
                "UpdatedDateTime": "2021-05-03T07:45:21.5115588Z",
                "Metadata": null
            }
        ]

To display the data, I am having class DisplayActivtiesResponse.cs below:为了显示数据,我在下面有 class DisplayActivtiesResponse.cs

public class DisplayActivtiesResponse
{
public IEnumerable<DisplayActivity> DisplayActivties{ get; set; }
}

public class DisplayActivity
{
public Guid ActivityId { get ;set; }
public DateTime ActivityDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
public IEnumerable<MetaData> MetaData { get; set; }
}

public class MetaData
{
public string Name { get; set; }
public string Value { get; set; }
}

Below is the code to fetch the data from DB:下面是从数据库中获取数据的代码:

 public async Task<DisplayActivtiesResponse> DisplayActivityAsync(Guid activityId)
        {
            var feedIterator = this.cosmosReviewRequestContainer.GetItemLinqQueryable<ActivitiesDB>()
                .Where(q => q.Activities.Where(x => x.ActivityId == activityId)
                .ToFeedIterator();
            var requestModel = new ActivitiesDB();
            while (feedIterator.HasMoreResults)
            {
                var items = await feedIterator.ReadNextAsync().ConfigureAwait(false);
                requestModel = items.FirstOrDefault();
            }

            return new DisplayActivtiesResponse
            {
                DisplayActivties = mapper.Map<IEnumerable<DisplayActivity>>(requestModel);
            };
        }

MappingProfile.cs:映射配置文件.cs:

 public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            this.CreateMap<Activity, DisplayActivity>().ReverseMap();
        }
    }

Now, When I am fetching the data from db, I observed that if the metadata is null, I am getting empty array [] instead of null .现在,当我从数据库中获取数据时,我观察到如果元数据是 null,我得到的是空数组[]而不是null I still dont know what i am missing from my end.我仍然不知道我到底错过了什么。 Kindly help me on this.请帮助我。

Set AllowNullCollections to true when initializing:初始化时将AllowNullCollections设置为true

AutoMapper.Mapper.Initialize(cfg =>
{
    cfg.AllowNullCollections = true;
});

or directly in the mapper configuration:或直接在映射器配置中:

new MapperConfiguration(cfg =>
{
    cfg.AllowNullCollections = true;
}

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

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