Matthew
Matthew

Reputation: 55

grouping string with linq to xml

I have some string like this:

04:51 05:09 05:29_ 05:49 06:09 06:29 06:49_ 07:09 07:25 07:40 07:55 08:10_ 08:25 08:40 08:55 09:10 09:25_ 09:40 09:55 10:10 10:25 10:40_ 10:55 11:10 11:25 11:40 11:55_ 12:10 12:25 12:40 12:55 13:10_ 13:25 13:40 13:55 14:10 14:25_ 14:40 14:55 15:10 15:25 15:40_ 15:55 16:10 16:25 16:40 16:55_ 17:10 17:29# 17:49# 18:09# 18:29#_ 18:49# 19:09# 19:29# 19:49#_ 20:09# 20:29# 20:49# 21:09#_ 21:29# 21:49# 22:09# 22:29#_ 22:51# 23:12#

And I need to group string above by hour to get xml like this:

<departure hour="04">
<min>51</min>
</departure>
<departure hour="05">
<min>09 29_ 49</min>
</departure>

etc.

I've tried some group sth by sth into sth statments for linq but nothing satisfying so far...

Ok, now I have something like this for spliting:

 var dep = parts[2].Split(' ').Select(p => new XElement("departure",    new XAttribute("hour", p.Split(':')[0]), new XElement("min",p.Split(':')[1])));

which gives me something like this for example:

<departure hour="04">
      <min>49</min>
    </departure>
    <departure hour="05">
      <min>07</min>
    </departure>
    <departure hour="05">
      <min>27_</min>
    </departure>
    <departure hour="05">
      <min>47</min>
    </departure>

the problem is code for grouping...

EDIT:

I managed to write something like this:

 var depGrouped = from grouped in dep
                  group grouped by new {h = (string)grouped.Attribute("hour")} into g 
                  select new XElement("departure", new XAttribute("hour", g.Key.h), new XElement("min", g));

but it produced not quite what I expected for example:

<departure hour="05">
    <min>
      <departure hour="05">
        <min>04</min>
      </departure>
      <departure hour="05">
        <min>24</min>
      </departure>
      <departure hour="05">
        <min>44</min>
      </departure>
    </min>
  </departure>
  <departure hour="06">
    <min>
      <departure hour="06">
        <min>06_</min>
      </departure>
      <departure hour="06">
        <min>24</min>
      </departure>
      <departure hour="06">
        <min>44</min>
      </departure>
      <departure hour="06">
        <min>58</min>
      </departure>
    </min>
  </departure>

Can you help me to exlude some of info from output to match example from top of post?

Upvotes: 0

Views: 105

Answers (2)

Phil
Phil

Reputation: 42991

Try this, works for me in LINQPad.

var s = "04:51 05:09 05:29_ 05:49 06:09 06:29 06:49_ 07:09 07:25 07:40 07:55 08:10_ 08:25 08:40 08:55 09:10 09:25_ 09:40 09:55 10:10 10:25 10:40_ 10:55 11:10 11:25 11:40 11:55_ 12:10 12:25 12:40 12:55 13:10_ 13:25 13:40 13:55 14:10 14:25_ 14:40 14:55 15:10 15:25 15:40_ 15:55 16:10 16:25 16:40 16:55_ 17:10 17:29# 17:49# 18:09# 18:29#_ 18:49# 19:09# 19:29# 19:49#_ 20:09# 20:29# 20:49# 21:09#_ 21:29# 21:49# 22:09# 22:29#_ 22:51# 23:12#";

var times = s.Split(new char[]{' '}).Select (x => x.Trim());

var byHour = times.Select (
     t => t.Split(new char[]{':'})).Select(t => new {hour=t[0], minutes=t[1]});

var grouped = byHour.GroupBy (h => h.hour).Select (
    h => new {hour=h.Key, minutes=string.Join(" ", h.Select (x => x.minutes))});

var xml = grouped.Select (a => 
    new XElement("departure", 
    new XAttribute("hour", a.hour),
    new XElement("min", a.minutes)
    ));

xml.Dump();

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

I have no chance for trying this right now, but idea is simple:

var dep = from p in parts[2].Split(' ')
          let p_parts = p.Split(':')
          let hour = p_parts[0]
          let min = p_parts[1]
          group p by hour into g
          select new XElement("departure",
                              new XAttribute("hour", g.Key),
                              (from m in g select new XElement("min", m.min));

Upvotes: 0

Related Questions