Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

C# Looping through a list and extracting specific data

I need to loop through a list of type TrackRecordVM and extract data from fields that are only assigned to fields names Jan, Feb, Mar,Apr, May, Jun, July,Aug,Sep,Oct,Nov,Dec

This is the list that I am looping through

public class TrackRecordVM
{
    public int Year { get; set; }
    public decimal? Jan { get; set; }
    public decimal? Feb { get; set; }
    public decimal? Mar { get; set; }
    public decimal? Apr { get; set; }
    public decimal? May { get; set; }
    public decimal? Jun { get; set; }
    public decimal? Jul { get; set; }
    public decimal? Aug { get; set; }
    public decimal? Sep { get; set; }
    public decimal? Oct { get; set; }
    public decimal? Nov { get; set; }
    public decimal? Dec { get; set; }
    public decimal? YTD { get; set; }
    public decimal? Bmrk { get; set; }
    public string BmrkName { get; set; }
}

Here is the loop. Whats the best way of doing it

 List<TrackRecordVM> trackRecordVm;

            foreach (var trackRecord in fundTrackRecord)
            {
                trackRecordVm = trackRecord.TrackRecord;

                foreach (var track in trackRecordVm)
                {


                }
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'd probably have an easier time with Linq.

Something along the lines of this (just repeat this for whichever members you need):

var jan = trackRecordVm.Select(x => x.Jan).ToList();

var dec = trackRecordVm.Select(x => x.dec).ToList();

var newList = new List<decimal?>(); newList.AddRange(jan); newList.AddRange(dec);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...