C# How to create a list of classes

43,642

Solution 1

You can do this easily with LINQ:

var list = dataViewer
    .Select(item => new YourClass
    {
        StringProperty = ...,
        DateTimeProperty1 = ...,
        DateTimeProperty2 = ...
    })
    .ToList();

It lets you state your intentions (create a list of YourClass objects from each item in dataViewer) without emphasizing the mechanics behind it (loops, etc.)

Edit: If you don't require a list, just a sequence, this also looks nice using the query syntax (same meaning):

var yourClasses =
    from item in dataViewer
    select new YourClass
    {
        StringProperty = ...,
        DateTimeProperty1 = ...,
        DateTimeProperty2 = ...
    };

Solution 2

Maybe something like this

var list = new List<YourClass>();

foreach(var item in dataViewer) {
    var cls = new YourClass();
    // Assign variables here
    // cls.Test = item.Test;

    list.Add(cls);
}

Solution 3

Try this:

public class YourClass
{
    public string YourString {get; set;}
    public DateTime YourDate1 {get; set;}
    public DateTime YourDate2 {get; set;}

    public YourClass(string s, DateTime d1, DateTime d2)
    {
        YourString = s;
        YourDate1 = d1;
        YourDate2 = d2;
    }
}

public List<YourClass> Read()
{
    List<YourClass> list = new List<YourClass>();
    foreach(var item in dataViewer)
        list.Add(new YourClass(s,d1,d2)); // Read variables from item...
    return list;
}

Solution 4

public class Appropriate
{
    public string Value { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

IList<Appropriate> list = new List<Appropriate>();

foreach(var item in dataViewer) {
    list.Add(new Appropriate() {
        Value = item["value"],
        Start = item["start"],
        End = item["end"]
    });
}

IList<Appropriate> list = new List<Appropriate>();

dataViewer.ToList().ForEach(i => list.Add(new Appropriate() {
    Value = item["value"],
    Start = item["start"],
    End = item["end"]
});

Solution 5

public class Foo
{
    public Foo(string name, DateTime dt1, DateTime dt2)
    {
        Name = name;
        DT1 = dt1;
        DT2 = dt2;
    }

    public string Name { get; set; }
    public DateTime DT1 { get; set; }
    public DateTime DT2 { get; set; }
}

public class Example
{
    public List<Foo> example(DataView dataViewer)
    {
        var foos = new List<Foo>();        

        foreach(var data in dataViewer)
        {
            foos.Add(new Foo(data.Name, data.DT1, data.DT2);
        }

        return foos;
    }
}
Share:
43,642
CodingIsAwesome
Author by

CodingIsAwesome

Updated on July 09, 2022

Comments

  • CodingIsAwesome
    CodingIsAwesome almost 2 years

    I am new to c#, but not to programming. First of, thank you so much for your help!

    I want a class or struct whichever is appropriate which has 3 variables. A string, and two datetimes.

    I want to create a loop which stores new classes in a list.

    Something like:

    for each item in dataViewer
    create new class
    assign variables
    store class in list
    next

    Thank you so much for your help