开发者

Linq to collection (where clause)

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.

Thanks.

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();
                xDoc.LoadXml(node.OuterXml);
                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/el开发者_StackOverflow社区ement[@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

    Node.Attributes["enc"].Value

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


I got this code to work:

var strValues = 
@"notUnique|0|endofstring,
notUnique|1|endofString,
notUnique|2|endofString,
Unique1|0|endofString";

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)
{
    Console.WriteLine(t);
}

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.


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]) };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜