none
How to manipulate with list data C# LISTS - updated question

    Question

  • Ok,

    I will put here two classes and the rest of the code.

    In VS -> C# -> ConsoleApplication -> create two classes:

    GroupList.cs

            
    class GroupList
            {
                public string Location { get; set; }
                public string Date { get; set; }
                public string In { get; set; }
                public string Out { get; set; }
                public string Difference { get; set; }
                public string State { get; set; }
                public GroupList(string location, string date, string in_, string out_, string diff_, string statte_)
                {
                    Location = location;
                    Date = date;
                    In = in_;
                    Out = out_;
                    Difference = diff_;
                    State = statte_;
                }
                public override string ToString()
                {
                    return Location + ";" + Date + ";" + In + ";" + Out + ";" + Difference + ";" + State + ";";
                }
            }

    InOut.cs

        
    class InOut
        {
                public string DeviceName { get; set; }
                public string In { get; set; }
                public string Out { get; set; }
                public string Date { get; set; } //ovo je kao primary key
     
                public InOut(string devName_, string in_, string out_, string date_)
                {
                    DeviceName = devName_;
                    Date = date_;
                    In = in_;
                    Out = out_;
                }
                public override string ToString()
                {
                    return DeviceName + ";" + In + ";" + Out + ";" + Date;
                }
     
            }

    Program.cs:

            
    static void Main(string[] args)
            {
                var inOutClassList = new List<InOut>();
                var GroupList = new List<GroupList>();
    
                GroupList.Add(new GroupList("Location1","1.2.2017. 9:00:00","236","163","73","242"));        
                GroupList.Add(new GroupList("Location1","1.2.2017. 10:00:00","329","266","63","305"));        
                GroupList.Add(new GroupList("Location1","1.2.2017. 11:00:00","361","360","1","306"));        
                
                inOutClassList.Add(new InOut("01","22","3422","1.2.2017. 9:00:00"));
                inOutClassList.Add(new InOut("01","672","0","1.2.2017. 10:00:00"));
                inOutClassList.Add(new InOut("01", "55", "23", "1.2.2017. 11:00:00"));
                inOutClassList.Add(new InOut("02", "123", "321", "1.2.2017. 11:00:00"));
                inOutClassList.Add(new InOut("03", "900", "342", "1.2.2017. 10:00:00"));
                inOutClassList.Add(new InOut("04", "900", "342", "1.2.2017. 9:00:00"));
     
                pp(GroupList, inOutClassList);
            }
    
            private static void pp(List<GroupList> groupList, List<InOut> inOutList)
            {
                var distinctDates = inOutList.Select(x => x.Date).Distinct();
                var distinctCameras = inOutList.Select(x => x.DeviceName).Distinct();
                var OnlyDatesInGroups = groupList.Select(x => x.Date);
                var sortedDistinctDates = distinctDates.OrderBy(x => DateTime.Parse(x)).ToList();
                var sortedCameras = distinctCameras.OrderBy(x => x).ToList();
                var loger = "\n";
                loger += "Location;Date;In;Out;Diff;State" + "\n";
     
                 foreach (var g in groupList)
                {
                    var datum = g.Date.ToString(); //1.2.2017. 0:00:00
     
                    loger += g.Location.ToString() + ";" +
                        g.Date.ToString() + ";" +
                        g.In.ToString() + ";" +
                        g.Out.ToString() + ";" +
                        g.Difference.ToString() + ";" +
                        g.State.ToString() + "==";
                    foreach (var dev_inDetail in inOutList)
                    {
                        foreach (var k in sortedCameras)
                        {
                            //here is the problem, I don't know how to do it,
                            //I'm don't have logic currently
     
                            var kam = k.ToString();
                            if ((kam.ToString() == dev_inDetail.DeviceName.ToString()) && (datum == dev_inDetail.Date.ToString()))
                            {
     
                                loger += dev_inDetail.In.ToString() + ";" + dev_inDetail.Out.ToString() + ";";
                            }
                        }
                        //loger += "0;0;";
                    }
                    loger += "\n";
                }

    
    

    This is my desired output what should look like and my question:

                

    //desired Output

    //Location;Date;In;Out;Diff;State==Dev01;Dev02;Dev03;Dev4

    //Location1;1.2.2017. 9:00:00;236;163;73;242==22;3422; 0;0; 0;0; 900;342;

    //Location1;1.2.2017. 10:00:00;329;266;63;305==672;0; 0;0; 900;342; 0;0;//Location1;1.2.2017. 11:00:00;361;360;1;306==55;23; 123;321; 0;0; 0;0;

                                                      

    I want to print all devices values.For devices that don't have data for current time it should print out zeores 0;0;




    • Edited by bobis123 Friday, April 14, 2017 9:38 AM
    Friday, April 14, 2017 9:16 AM

Answers


  • foreach (var kvp in distinctDates)
    {
        var time = kvp.ToString();
        foreach (var groupItem in groupList)
        {
            if (groupItem.Date.Contains(time)) //group list
            {
                loger += groupItem.Location.ToString() + "  " + groupItem.Date.ToString() + "  ";
                foreach (var item3 in distinctCameras)
                {
                    var cam = item3.ToString();
                    var selectDate = inOutList.FindAll(x => x.Date.Contains(time));
                    foreach (var item in selectDate)
    	            {
                        if (item.DeviceName == cam)
                        {
                            loger += item.In.ToString() + "  " + item.Out.ToString() + "  " + groupItem.Difference.ToString() + "  " + groupItem.State.ToString() + "  ";
                        }
    	            }
                }
                loger += "\n";
            }
        }
    }

    • Edited by bobis123 Thursday, April 20, 2017 8:34 AM
    • Marked as answer by bobis123 Thursday, April 20, 2017 9:59 AM
    Friday, April 14, 2017 11:24 AM

All replies

  • Hello,

    I have two lists:

    First one with headers:

    Location;Date;In;Out;Minus;Plus;Temperature

    Location;4.1.2017 0:00:00;8;7;1;17;45

    Location;4.1.2017 1:00:00;78;71;9;149;45

    Location;4.1.2017 2:00:00;2;3;-1;5;45

    ...

    Second list has:

    ID;Date;In;Out

    Car1;1:00:00;72;70

    Car1;2:00:00;1;1

    Car2;0:00:00;8;7

    Car2;1:00:00;6;1

    Car2;2:00:00;1;2

    How can I present data using this two lists like this:

    Location;Date;In;Out;Difference;State;Temperature;Car1;Car2

    Location;4.1.2017 0:00:00;8;7;9;17;45;0;0;8;7

    Location;4.1.2017 1:00:00;78;71;9;149;45;72;70;6;1

    Location;4.1.2017 2:00:00;2;3;-1;5;45;1;1;1;2

    Notice zero values added for car1 at 0:00:00.Big thankx



    Or just use second list to create that list...
    Thursday, April 13, 2017 1:25 PM
  • The data you posted doesn't clearly identify the rules you're trying to follow to merge these lists together. Please identify exactly how you are determining that arbitrary row 1 in the first list gets mapped to arbitrary row 2 in second list. Also post the rules for determining the value of each value in the resultant row.
    Thursday, April 13, 2017 1:59 PM
    Moderator
  • Hi bobis123,

    Thank you for posting here.

    For your question, could you provide more details about your question?

    I make a generic list try to show the list you want, but in the list1 and list2, there are no definition for Difference. What is the difference? In the LISTS which you want, the Car1 and Car2 was in the list 2 under ID. If you want to do something like combine two lists, Car1 and Card2 are not the same level like ID, Date, In or other herders.

    For your question, if you want to add list1 to the end list2, you could use List<T>.AddRange Method.

    If you want to insert the list2 to list1 at the specified index, please try the List<T>.InsertRange Method.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 14, 2017 8:26 AM
    Moderator
  • I cannot explain, but I moved and updated this question here:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6b8925ff-1950-4f12-9fbc-6a4541cf76be/how-to-manipulate-with-list-data-c-lists-updated-question?forum=csharpgeneral

    Friday, April 14, 2017 9:17 AM
  • I cannot explain, but I moved and updated this question here:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6b8925ff-1950-4f12-9fbc-6a4541cf76be/how-to-manipulate-with-list-data-c-lists-updated-question?forum=csharpgeneral
    Friday, April 14, 2017 9:17 AM
  • Hi bobis123,

    Please click the red to insert your code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 14, 2017 9:20 AM
    Moderator

  • foreach (var kvp in distinctDates)
    {
        var time = kvp.ToString();
        foreach (var groupItem in groupList)
        {
            if (groupItem.Date.Contains(time)) //group list
            {
                loger += groupItem.Location.ToString() + "  " + groupItem.Date.ToString() + "  ";
                foreach (var item3 in distinctCameras)
                {
                    var cam = item3.ToString();
                    var selectDate = inOutList.FindAll(x => x.Date.Contains(time));
                    foreach (var item in selectDate)
    	            {
                        if (item.DeviceName == cam)
                        {
                            loger += item.In.ToString() + "  " + item.Out.ToString() + "  " + groupItem.Difference.ToString() + "  " + groupItem.State.ToString() + "  ";
                        }
    	            }
                }
                loger += "\n";
            }
        }
    }

    • Edited by bobis123 Thursday, April 20, 2017 8:34 AM
    • Marked as answer by bobis123 Thursday, April 20, 2017 9:59 AM
    Friday, April 14, 2017 11:24 AM
  • Hi bobis123,

    If you could how you solve your issue this will help others also.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, April 14, 2017 12:19 PM
    Moderator