Linq to collection(where子句)

时间:2011-09-07 16:15:08

标签: c# linq

我试图在集合中获取最新添加的版本。在这个集合中是'|' (管道分隔的字符串...)其中一个管段具有较大的数字。我试图得到一个拥有所有这些字符串数量最多的独特集合......听起来这可能是一个巨大的交易,我不确定linq是否可以处理这个问题。 (我没有那么多使用Linq,所以我不确定它能做什么和不能做什么。这样做甚至可能吗?如果是这样,你会如何实现它?需要简单的算法,不需要对于代码,我只需要知道我需要去哪里。

感谢。

更新:我正在尝试做的代码示例...

    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/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());

如您所见,我目前有一个列表,从该集合中获取唯一条目。我希望这个独特的条目也有最高的

    Node.Attributes["enc"].Value

您可以看到哪个是字符串中的一个管道。我想知道这是否可能?

更新II 这是我试图做到的一种方式,这只是我试图掌握linq ......

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

更新3:目标明确 我基本上需要一个唯一的selectListItem列表,该列表的每个成员都有这个最大值......

UPDate简单示例

我将只有一个简单的示例,它不会包含我之前发布的管道分隔字符串中的所有字段。毕竟这只是一个例子......

SelectListItem我有

    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

    Text: Value1, Value: notUnique|2|endofstring
    Text: Value1, Value: Unique1|0|endofstring

2 个答案:

答案 0 :(得分:2)

我得到了这段代码:

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

您可以将其用于您需要的工作。前几行是我只是模仿从csv读取值。对于代码的质量感到抱歉,我没有时间进行清理或将其塑造得更接近您上面发布的代码。

答案 1 :(得分:1)

最佳猜测取自http://msdn.microsoft.com/en-us/vcsharp/aa336747#maxGrouped

完全未经测试。可能包含错误。

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