如何将数组中的元素引用到多个其他值

本文关键字:其他 引用 元素 数组 | 更新日期: 2023-09-27 17:53:51

我想编写一个程序,读取(图书标题的)文本文件,并将每个图书标题与用户评分相对应。如何为数组中的图书标题分配多个评级呢?

到目前为止,

class Program
{
    static void Main(string[] args)
    {
        string[] lines = File.ReadAllLines("books.txt");
    }

用户评分以这种形式在一个单独的文本文件中:

User:a
2 
5 
4 
1
1
User:b
5
5
0
1
2

如果有5本书和两个用户,我如何将2分和5分与一本名为"the Hunger Gamer"的书联系起来?

如何将数组中的元素引用到多个其他值

似乎这里有3个数据:Book, User和Rating。你可以为每一个元素创建结构,并用适当的数据填充它们,然后你可以用它们做各种有趣的事情。

下面是一个例子:

public class Book
{
    public int ID;
    public string Name;
}
public class User
{
    public int ID;
    public string Name;
}
public class Rating
{
    public int UserID;
    public int BookID;
    public int Value;
}

从基础级开始,您可以将这些项的列表加载到数组中:

Book[] books = new Book[] 
    { 
        new Book { ID = 1, Name = "A B C" },
        new Book { ID = 2, Name = "Word Two" },
        new Book { ID = 3, Name = "Third Book" },
        new Book { ID = 4, Name = "And Another" },
        new Book { ID = 5, Name = "The Last Word" }
    };
User[] users = new User[] 
    { 
        new User { ID = 1, Name = "A" },
        new User { ID = 2, Name = "B" },
    };
Rating[] ratings = new Rating[] 
    {
        new Rating { UserID = 1, BookID = 1, Value = 2 },
        new Rating { UserID = 1, BookID = 2, Value = 5 },
        new Rating { UserID = 1, BookID = 3, Value = 4 },
        new Rating { UserID = 1, BookID = 4, Value = 1 },
        //new Rating { UserID = 1, BookID = 5, Value = 1 },
        new Rating { UserID = 2, BookID = 1, Value = 5 },
        new Rating { UserID = 2, BookID = 2, Value = 5 },
        new Rating { UserID = 2, BookID = 3, Value = 0 },
        new Rating { UserID = 2, BookID = 4, Value = 1 },
        //new Rating { UserID = 2, BookID = 5, Value = 2 },
    };

现在来点有趣的…

您的收藏中所有书籍的平均评级列表(包括那些没有评级的,我在ratings初始化器中注释掉的):

var bookRatings = 
// start with books
    from bk in books
// get all the ratings related to each book...
    join r in ratings on bk.ID equals r.BookID into _ratings
// put a default value (null, no value) when no ratings were found
    from r in _ratings.DefaultIfEmpty()
// now group the ratings by book
    group (r == null ? null : (int?)r.Value) by bk into grp
// and now do some calculations:
    select new 
        {
            grp.Key.ID,
            grp.Key.Name,
            Rating = grp.Average(),
            NumRatings = grp.Count(v => v != null)
        };
// Now display it:
foreach (var result in bookRatings)
    Console.WriteLine("{0}'t{1,-15}'t{2}'t{3}", result.ID, result.Name, result.Rating, result.NumRatings);

输出如下:

1   A B C           3.5 2
2   Word Two        5   2
3   Third Book      2   2
4   And Another     1   2
5   The Last Word       0

我已经使用了类、数组、匿名类型和一些相当有趣的LINQ表单,但这些都是值得学习的好东西。点击链接,阅读一下,直到你理解了上面发生的大部分事情。

顺便说一下,如果我把数据存储在数据库中,上面的代码几乎就是我要做的——整体结构相同,代码也几乎相同。
LINQ to SQL省去了很多实现过程中的痛苦。

您可以在这里使用Dictionary。这样的

Dictionary<string, List<int>> bookMapping = new Dictionary<string, List<int>>();
bookMapping.Add("youruserkey", new List<int> { 1, 2, 3, 4, 5 });

PS:请详细阅读链接,了解其工作原理。