C# Linq到集合(where子句)

C# Linq到集合(where子句),c#,linq,C#,Linq,我正在尝试获取集合中某个内容的最后添加版本。此集合中有一个“|”(管道分隔字符串…)其中一个管道段的编号较高。我正在尝试获得一个唯一的集合,它拥有所有这些字符串中数量最多的字符串。。。这听起来可能是一件大事,我不确定林克是否能处理好这件事。(我不太使用Linq,所以我不确定它能做什么和不能做什么。做这种事情可能吗?如果可能,你将如何实现它?需要简单的算法,不需要代码,我只需要知道我需要去哪里做 谢谢 更新:我正在尝试做的代码示例… List<SelectListItem> P

我正在尝试获取集合中某个内容的最后添加版本。此集合中有一个“|”(管道分隔字符串…)其中一个管道段的编号较高。我正在尝试获得一个唯一的集合,它拥有所有这些字符串中数量最多的字符串。。。这听起来可能是一件大事,我不确定林克是否能处理好这件事。(我不太使用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());
正如你所看到的,这是管柱中的一根管子。我想知道这是否可能

更新II 这是我尝试的一种方法,这只是我试图很好地掌握linq

    PatientList.GroupBy(x=>x.Value.Split('|')[1].Max).Select(x => x.FirstOrDefault());
更新3:目标澄清 我基本上需要一个独特的selectListItems列表,该列表中的每个成员都具有最大值

更新简单示例

我将有一个简单的例子,它不会包含我之前发布的管道分隔字符串中的所有字段

选择我拥有的物品

    Text: Value1, Value: notUnique|0|endofstring
    Text: Value2, Value: notUnique|1|endofString
    Text: Value3, Value: notUnique|2|endofString
    Text: Value4, Value: Unique1|0|endofString
选择我想要的物品

    Text: Value1, Value: notUnique|2|endofstring
    Text: Value1, Value: Unique1|0|endofstring
最佳猜测来自

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

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

我使用了这个代码:

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读取值。对于代码的质量,我很抱歉,我没有时间清理它或使它更接近您上面发布的代码。

这应该是可能的,但您可能应该显示一些示例代码和/或数据,以便我们知道确定您的要求是什么。您能否澄清“获取具有所有这些字符串中数量最多的唯一集合”的含义我可以将其解释为每个集合成员的最大值,也可以解释为整个集合的最大值。我从中选择的列表有几个成员,这些成员仅通过管道分隔字符串的第二个值来区分。此列表中有几个不同的成员(每个成员可能出现多次).我想要一个只有其中一个成员的列表(即唯一列表)由管道分隔字符串的第二个值决定。我建议您发布一个示例列表,而不是发布代码。我不确定这是否是一个打字错误,但selectlistitem中的第二个文本值应该是Value4吗?新颖的解决方案。我原以为可以,但恐怕没有雪茄。我得到了完全相同的结果选择我开始时使用的列表。这是一个好的开始。我需要以某种方式改进此列表。如果我在linq方面稍有进步,这将不会是一项艰巨的任务。别担心。正如我所说,我只需要某种算法来让我的思维活跃起来。这再次看起来是一个很好的解决方案。我将尝试一下。。。
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);
}