Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数组中的元素引用到多个其他值?_C# - Fatal编程技术网

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

C# 如何将数组中的元素引用到多个其他值?,c#,C#,我想做一个程序,读取一个文本文件(书名),并将每个书名对应到一个用户评级。如何为数组中的一个书名分配多个评级 到目前为止, 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本书和

我想做一个程序,读取一个文本文件(书名),并将每个书名对应到一个用户评级。如何为数组中的一个书名分配多个评级

到目前为止,

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分与一本叫做《饥饿玩家》的书联系起来?

你可以在这里使用一个。像这样

Dictionary<string, List<int>> bookMapping = new Dictionary<string, List<int>>();
bookMapping.Add("youruserkey", new List<int> { 1, 2, 3, 4, 5 });
Dictionary bookMapping=newdictionary();
添加(“youruserkey”,新列表{1,2,3,4,5});

PS:一定要详细阅读该链接的工作原理。

这里似乎有3条数据:书籍、用户和评级。您可以为每一个应用程序创建结构,并用适当的数据填充它们,然后您可以用它们做各种有趣的事情

下面是一个例子:

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 },
    };
现在来找点乐子

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

以下是输出:

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

我用过,还有一些相当有趣的表格,但都是值得学习的好东西。点击链接并阅读,直到你理解上面发生的大部分事情


顺便说一句,如果我将数据存储在数据库中,上面的内容几乎就是我要做的事情——总体结构相同,代码也几乎相同。当您进行转换时,消除了很多实现的痛苦。

您想在内存或数据库中进行转换吗?对不起,我真的不熟悉编程有什么区别?这并不能回答我的问题。您想将这些值存储在某个数据库中,还是只存储在程序中而不涉及任何数据库?
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