Take the 2-minute tour ×

I am attempting to get the last added version of something in a collection. In this collection is a '|' (pipe delimited string...) one of the pipe segments has a high number. I am attempting to get a unique collection that has the highest number of all those strings... This sounds like it might be a mighty big deal, and I am not sure if linq can handle this. (I don't use Linq that much so I am not to sure of what it can and can't do. Is doing this sort of thing even possible? If so, how would you implement it? Simple algorithm is needed, no need for code, I just need an idea of where I need to go for this.


UPDATE: code example of what I am trying to do...

    List<SelectListItem> PatientList = new List<SelectListItem>();
            foreach (XmlNode node in nodeList)
                XmlDocument xDoc = new XmlDocument();
                string popPatInfo = xDoc.SelectSingleNode("./template/elements/element[@name=\"FirstName\"]").Attributes["value"].Value + ", " + xDoc.SelectSingleNode("./template/elements/element[@name=\"LastName\"]").Attributes["value"].Value + " | " + DateTime.Parse(xDoc.SelectSingleNode("./template/elements/element[@name=\"DateOfBirth\"]").Attributes["value"].Value.Split('T')[0]).ToString("dd-MMM-yyyy");
                string patientInfo = xDoc.SelectSingleNode("./template/elements/element[@name=\"PatientId\"]").Attributes["value"].Value + "|" + xDoc.SelectSingleNode("./template/elements/element[@name=\"PopulationPatientID\"]").Attributes["enc"].Value + "|" + xDoc.SelectSingleNode("./template/elements/element[@name=\"AdminDate\"]").Attributes["value"].Value;
                PatientList.Add( new SelectListItem { Text = popPatInfo, Value = patientInfo });
            var queryResults = PatientList.GroupBy(x => x.Value).Select(x => x.FirstOrDefault());

As you can see, I currently have a list that gets a unique entry from that collection. I want that unique entry to also have the highest


Which is one of the pipes in the string as you can see. I am wondering if this is even possible?

Update II Here is one way I attempted to do it, and this is just me trying to get a good grasp on linq...

    PatientList.GroupBy(x=>x.Value.Split('|')[1].Max).Select(x => x.FirstOrDefault());

UPDATE 3: Goal clarification I essentially need a list of unique selectListItems each member of that list has that maximum value...

UPDate Simple example

I will just have a simple example that won't have all the fields in the pipe delimited string that I posted earlier. This is just an example after all...

SelectListItem I have

    Text: Value1, Value: notUnique|0|endofstring
    Text: Value2, Value: notUnique|1|endofString
    Text: Value3, Value: notUnique|2|endofString
    Text: Value4, Value: Unique1|0|endofString

SelectListItem I want

    Text: Value1, Value: notUnique|2|endofstring
    Text: Value1, Value: Unique1|0|endofstring
share|improve this question
It should be possible, but you should probably show some example code and/or data so that we know for sure what your requirements are. –  LukeH Sep 7 '11 at 16:19
Please show an example –  driis Sep 7 '11 at 16:19
Can you clarify what is meant by "get a unique collection that has the highest number of all those strings" I could interpret this as either the max per collection member or the max overall –  Conrad Frix Sep 7 '11 at 16:19
Instead of posting code, I suggest you to post a sample list, and the desired result. –  Francisco Sep 7 '11 at 19:32
Not sure if its a typo or not but should the second text value in the selectlistitem you want be Value4 instead? –  Gage Sep 7 '11 at 19:56

2 Answers 2

up vote 2 down vote accepted

I got this code to work:

var strValues = 

var values = strValues.Split(',');

var newValues = values.Select(x =>
    var y = x.Split('|');
    return new { Category = y[0], Value = y[1], TheString = y[2] };

var test = newValues.GroupBy(p => p.Category)
                    .Select(g => new 
                                    Category = g.Key,
                                    Value = g.Max(p => p.Value) 

foreach(var t in test)

You may be able to work that into what you need. The first few lines are me just emulating reading the values from a csv. Sorry about the quality of the code, I don't have time to clean it up or mold it closer to the code you've posted above.

share|improve this answer
Don't sweat it. Like I said, I just need some kind of algorithm to get me mental juices going. This again looks like a good solution. I will give it a shot... –  DmainEvent Sep 7 '11 at 20:53

Best guess taken from http://msdn.microsoft.com/en-us/vcsharp/aa336747#maxGrouped

Completely Untested. May contain errors.

var queryResults =
        from p in PatientList
        group p by p.Value into g
        select new SelectListItem { Text= g.Key, Value= g.Max(p => p.Value.Split('|')[1]) };
share|improve this answer
Novel Solution. I thought it would work, but no cigar I am afraid. I get the exact same select list that I started out with. Which is a good start. I will need to somehow refine this. If I was a little bit better at linq this wouldn't be such a herculean task. –  DmainEvent Sep 7 '11 at 20:40

Your Answer


