[英]Complex LINQ query
I know LINQ but my knowledge is pretty much only selects, where, orderby and all of the most common functions. 我知道LINQ,但我的知识几乎只有选择,位置,排序和所有最常见的功能。 Now I have a need to do something that I think is really difficult and maybe not even possible to do just with LINQ.
现在,我需要做一些我认为确实很困难的事情,甚至可能无法仅使用LINQ做某事。 What I have is a list of people.
我有一个人名单。 That's east to query but I need to create a text string from that list.
进行查询很容易,但是我需要从该列表中创建一个文本字符串。 The text string has to give a letter followed by the name of each person.
文本字符串必须带有字母,后跟每个人的名字。
IList<person> Person
I need to be able to have a LINQ statement that checks through the Person list. 我需要能够通过个人列表进行检查的LINQ语句。 I need to be able to look for names that appear more than once.
我需要能够查找出现多次的名称。 So far I have the following.
到目前为止,我有以下几点。 It works okay but doesn't give everything needed:
它可以正常工作,但不能提供所需的一切:
Person[0] name="Fred" &
Person[1] name="Pete" &
Person[2] name="Tony" the var abc = "a) Fred. b) Pete. c) Tony
var a = "";
foreach (var person in _persons
.Select((data, value) => new { Data = data, Value = value })
{
a = a + (char)(details.Value + 64) + details.name
}
What I need is the additional functionality so that: 我需要的是其他功能,以便:
Person[0] name="John" then var abc = "a) John."
Person[1] name="John" &
Person[3] name="John" then var abc = "b) & d) John."
Person[1] name="John" &
Person[2] name="John" &
Person[3] name="John" then var abc = "b),c) & d) John."
In other words, get the names and put a character before them that shows what position the name is in the list. 换句话说,获取名称并在其前面放置一个字符,以显示名称在列表中的位置。 However if the name appears twice then instead of a)name1.
但是,如果名称出现两次,则代替a)name1。 b)name1 I need to get a),b) name.
b)name1我需要获得a),b)名称。
It's something I can't really figure out how to do. 我真的不知道该怎么做。 I would appreciate any advice or pointers that anyone can give me.
任何人都可以给我的任何建议或指示,我将不胜感激。
I think I understand it, but I deserve a medal if I do. 我想我理解,但如果我这样做,我应该获得一枚奖牌。
foreach (var group in _persons
.Select((data, value) => new { Data = data, Value = value }
.GroupBy (x => x.Data))
{
foreach (var item in group)
a = a + (char)(item.Value + 64) + ") ";
a = a + group.Key;
}
Given: 鉴于:
var persons=new[] {"Fred", "John", "John", "Pete", "John"};
You can write: 你可以写:
char id = 'a';
foreach (var row in persons
.Select(w => new { id = id++, name = w })
.GroupBy(w => w.name)
.Select(w => w
.Select(ww => ww.id + ")")
.Aggregate((c, n) => c + "&" + n)
+ " " + w.Key))
{
Console.WriteLine(row);
}
And that gives you: 那给你:
a) Fred
b)&c)&e) John
d) Pete
What you need is to first group by the names, and then convert them to strings. 您需要首先按名称分组,然后将其转换为字符串。
var result = names
.Select((name, index) => new { Name = name, Prefix = (char)(index + 'a') + ")" })
.GroupBy(p => p.Name, p => p.Prefix)
.Select(g => string.Join(" & ", g) + " " + g.Key);
This example doesn't entirely do the formatting of the a), b) & c)
thing (instead it gives a) & b) & c)
, but it should get you started. 这个示例并没有完全完成
a), b) & c)
的格式化(相反,它给出a) & b) & c)
的格式,但是应该可以帮助您入门。
You can use Count() to achieve this. 您可以使用Count()实现此目的。 It can take a function to determine whether something should be counted.
它可以使用一个函数来确定是否应计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.