简体   繁体   English

如何将值插入列表集合

[英]How to insert values to a list collection

I need to insert elements into my empty list collection as one array item that contains Card and Date.我需要将元素作为一个包含卡片和日期的数组项插入到我的空列表集合中。

TimeOut Justification is my entity class that contains parameters like Card and Date TimeOut Justification 是我的实体类,其中包含 Card 和 Date 等参数

I'm checking if TimeOutArray is 0, then i need to insert Card and Date.我正在检查 TimeOutArray 是否为 0,然后我需要插入卡和日期。

List<TimeOutJustification>TimeOutArray;

Result should look like this结果应该是这样的

[0] = {Card="1234",Date="12-03-19"};

It's not super clear to me what the scenario is here, but I am going to assume that the list TimeOutArray may or may not have been initialized, and may or may not have items.我不太清楚这里的场景是什么,但我将假设列表TimeOutArray可能已或可能未初始化,并且可能有也可能没有项目。 It sounds like you want to check for those things, and, if it does not have items, add one.听起来您想检查这些东西,如果没有项目,请添加一个。

First, initialize the list if it is null:首先,如果列表为空,则初始化列表:

if (TimeOutArray == null)
{
    TimeOutArray = new List<TimeOutJustification>();
}

Then, check if the item count is 0, and add the new item.然后,检查项目计数是否为 0,并添加新项目。

if (TimeOutArray.Count == 0)
{
    TimeOutArray.Add(new TimeOutJustification
    {
        Card = "1234",
        Date = "12-03-19"
    });
}

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

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