c#中的List集合 - 将文件添加到列表中 [英] Collection Of List in c# - add files to list

查看:216
本文介绍了c#中的List集合 - 将文件添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<EventFile> CollectionsOfEvtfiles = new List<EventFile>();

if (args.IsOffline)
{
    using (SqlConnection conn = new SqlConnection(Db.ConnectionString))
    {
        conn.Open();
        FileHelper.ImportFile(Entities, newEventFile, ImportMode.ManualMode, false, conn,IsCellCreated);
        if (newEventFile.RecordsCount!=0)
        {
            CollectionsOfEvtfiles.Add(newEventFile);
        }
    }
}



在我的代码中导入多个文件。它工作正常。但是当在列表中添加文件时,它仅在列表中添加最后一个文件。 (例如,我正在导入5个文件,但最后它只需要添加列表的最后一个文件)请帮助我..我想在列表中添加所有文件


In my code import multiple files. It working fine. But when add the files in list, It add last file only in list. (Ex. I am importing 5 files, But finally it take last file only to add list)Please help me.. i want to add all files in list

推荐答案

使用List的AddRange()方法代替Add。如下所示:

Use AddRange() method of List instead Add. Like below:
string[] input = { "Apple",
                           "Orange",
                           "Apricots" };

        List<string> fruits= new List<string>();
        Console.WriteLine("\nAddRange(fruits)");
        fruits.AddRange(input);

        Console.WriteLine();
        foreach( string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }        


你好Rnshanmugavadivel,



查看你的代码你正在检查

Hello Rnshanmugavadivel,

Seeing Your Code you are checking
if (newEventFile.RecordsCount!=0)
        {
            CollectionsOfEvtfiles.Add(newEventFile);//Error Point
        }





只需添加一次。

你运行循环

或ANotheir方式使用 AdddRange方法

添加指定的元素收集到列表的末尾。



参见示例&试一试



Simply you are adding only One Time..
Either you run the loop
or ANotheir way Use AdddRange Method.
which Adds the elements of the specified collection to the end of the List.

See The example & Try It

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    List<int> a = new List<int>();
    a.Add(1);
    a.Add(2);
    a.Add(5);
    a.Add(6);

    // Contains:
    // 1
    // 2
    // 5
    // 6

    int[] b = new int[3];
    b[0] = 7;
    b[1] = 6;
    b[2] = 7;
     //adding Multiple Items
    a.AddRange(b);

    // Contains:
    // 1
    // 2
    // 5
    // 6
    // 7 [added]
    // 6 [added]
    // 7 [added]
    foreach (int i in a)
    {
        Console.WriteLine(i);
    }
    }
}





希望它能帮助你。



Hope It Helps You.

这篇关于c#中的List集合 - 将文件添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆