简体   繁体   English

C#将列表作为逗号分隔的字符串存储在字符串类型变量中,该字符串类型变量具有列表中项目的出现次数

[英]C# Store A List As a Comma Separated String In String Type Variable With Number Of Occurrence Of Items In The List

I have a list List<Book> booksList of type Book 我有一个列表List<Book> booksList类型的Book

public class Book
{
public int ID {get; set;}
public string Name {get; set;}
public DateTime date {get; set;}

}

lets say we have 8 books in this list with following Names, book1, book2, book1, book4,book2, book4, book3, book4 . 假设我们在此列表中有8本书,其名称分别为book1, book2, book1, book4,book2, book4, book3, book4 As you see so many same book name with different ID is in the list. 如您所见,列表中有许多相同的书名,但ID不同。 I want to store similar book names with multiplication of number of occurrence of that book name in the list inside a string variable called string BooksName as a comma separated string, so when I retrieve BooksName i will have one comma separated string value like this 我想将相似的书名与该书名出现次数的乘积存储在列表中的字符串变量中,该变量称为string BooksName作为逗号分隔的字符串,所以当我检索BooksName我将有一个这样的逗号分隔的字符串值

"2 X book1 , 2 X book2 , 1 X book3 , 3 X book4"

Because I have 2 of book1, 2 of book2, 1 of book3 and 3 of book4 in the list. 因为我在列表中有book1的2,book2的2,book3的1和book4的3。

I tried following linq but it did not work 我尝试了以下linq,但是没有用

string BooksName = booksList.GroupBy(r =>r.Name).Select(s => s.Select(e =>e.Name).Count() + " X " + s.Name).ToString();

After How can I do that? 之后,我该怎么做?

Thank you professionals in advance 提前谢谢专业人员

You just need to order the results of GroupBy , modify how you are selecting the book name for display, and use string.Join() . 您只需要订购GroupBy的结果,修改您要选择的书籍名称进行显示的方式,然后使用string.Join()

void Main()
{
    var booksList = new List<Book>();
    booksList.Add(new Book { Name = "book1" });
    booksList.Add(new Book { Name = "book2" });
    booksList.Add(new Book { Name = "book1" });
    booksList.Add(new Book { Name = "book4" });
    booksList.Add(new Book { Name = "book2" });
    booksList.Add(new Book { Name = "book4" });
    booksList.Add(new Book { Name = "book3" });
    booksList.Add(new Book { Name = "book4" });

    string BooksName = string.Join(", ", booksList
        .GroupBy(r =>r.Name)
        .OrderBy(r => r.First().Name)
        .Select(s => s.Count() + " X " + s.First().Name));

    Console.WriteLine(BooksName);
    // Output: 2 X book1, 2 X book2, 1 X book3, 3 X book4
}

public class Book
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime date { get; set; }
}

You can't just .ToString an enumerable, you need to use string.Join instead:. 您不仅可以将.ToString枚举,还需要使用string.Join代替:

var booksList = "book1, book2, book1, book4,book2, book4, book3, book4".Split(',')
    .Select(x => new Book { Name = x.Trim() })
    .ToList();

var BooksName = string.Join(", ", booksList
    .GroupBy(x => x.Name)
    .OrderBy(g => g.Key)
    .Select(g => $"{g.Count()} X {g.Key}")
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM