Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

linq data sorted

Below codes is to return .net datatable, and I use asp.net repeater to bind it. everything is working except the list does not sort by date and time. The data source starts from query3.

Not sure why. and hope some experts can help out.

Thanks

var query3 = query1.Concat(query2).OrderBy(z => z.DateTimeSent);

           


             
public static DataTable GetConversation(string sid, string token,string accountPhoneNo, string twilioPhoneNo)
        {  
            string clientPhoneNo = string.Empty;
            string inputFilePath = filePathUserMessagesAll;
            DataTable dt = new DataTable();
            dt.Clear();
            dt.Columns.AddRange(new DataColumn[]
           {
                new DataColumn("Body",typeof(string)),
                new DataColumn("DateCreated",typeof(string)),
                new DataColumn("Direction",typeof(string)),
                new DataColumn("SelectedPhoneNo",typeof(string))
           });
            XDocument xdoc = null;
            using (XmlReader xr = XmlReader.Create(inputFilePath))
            {
                xdoc = XDocument.Load(xr);
                var query1 = from t in xdoc.Descendants("User")
                             where 
                             (
                             t.Element("From").Value.ToLower() == "+1" + accountPhoneNo.ToLower()
                             ) //client phone
                             select new
                             {
                                 Body = t.Element("Body").Value,
                                 DateTimeSent = t.Element("DateTimeSent").Value,
                                 Direction = t.Element("Direction").Value,
                                 To = t.Element("To").Value
                             };
                var query2 = from t in xdoc.Descendants("User")
                             where t.Element("To").Value.ToLower() == "+1" + accountPhoneNo.ToLower()
                             select new
                             {
                                 Body = t.Element("Body").Value,
                                 DateTimeSent = t.Element("DateTimeSent").Value,
                                 Direction = t.Element("Direction").Value,
                                 To = t.Element("From").Value 
                             };
                var query3 = query1.Concat(query2).OrderBy(z => z.DateTimeSent);
                foreach (var x in query3)
                {
                    dt.Rows.Add(x.Body,(DateTime.Parse(x.DateTimeSent)).ToString("MM/dd/yy h:mm", System.Globalization.CultureInfo.InvariantCulture),x.Direction,accountPhoneNo);
                }System.Globalization.CultureInfo.InvariantCulture);
                xr.Close();
                return dt;
            }
        }
       

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did that work out for you?
Solution posted.