简体   繁体   English

为什么我只使用where参数从数据库表中获取单个值?

[英]Why am I getting only a single value from a database table using where with parameters?

I have a table named "Registered_User" , which contains two attributes : 1. FavGenre 2. id_User. 我有一个名为"Registered_User"的表,它包含两个属性: 1. FavGenre 2. id_User.

One user may have more than one favourite genre. 一个用户可能有多个喜欢的类型。 The table data looks like this : 表数据如下所示:

 FavGenre  |  id_User
 1         |  2
 3         |  2
 5         |  2

What I'm basically trying to do, is to get all the favourites genres of user with an ID=2 . 我基本上想要做的是获得ID=2所有用户类型。 So my expected return in this case would be the numbers 1, 3 and 5 . 所以我在这种情况下的预期回报将是数字1, 3 and 5

I'm trying to use this sequence to get that result : var genres = db.Registered_User.Where(x => x.id_User == 2).ToList(); return View(genres); 我正在尝试使用此序列来获得该结果: var genres = db.Registered_User.Where(x => x.id_User == 2).ToList(); return View(genres); var genres = db.Registered_User.Where(x => x.id_User == 2).ToList(); return View(genres);

But the result I get is : 但我得到的结果是:

Genre |  User
1        2
1        2
1        2

Obviously that's not the correct result, it seems like it only takes the first FavGenre value and just repeats it. 显然这不是正确的结果,似乎只需要第一个FavGenre值并重复它。

So my question would be is the sequence I'm using not right for my purposes? 所以我的问题是我使用的序列不适合我的目的吗? Or does it seem there might be some kind of errors elsewhere (I was thinking that maybe there is something wrong the database or the exact table itself)? 或者似乎其他地方可能存在某种错误(我当时认为数据库或确切的表本身可能有问题)?

That is because the condition you gave "Where(x => x.id_User == 2)" will return the first results after the condition is met and not look any further. 这是因为您给出的条件“Where(x => x.id_User == 2)”将在满足条件后返回第一个结果,而不再查看。 That is since the condition meets the result that "user = 2" is "genre =1", it will only return that. 那是因为条件满足“user = 2”是“genre = 1”的结果,它只会返回那个。 So you have to use a loop to check all the corresponding values to "user=2". 因此,您必须使用循环来检查所有相应的值为“user = 2”。 I hope this was helpful. 我希望这可以帮到你。 :3 :3

暂无
暂无

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

相关问题 为什么在此查询中,对于From子句中的表,我得到的标识符无效? - Why am I getting an invalid identifier in this query for the table in the From Clause? 为什么使用LTRIM和RTRIM获得空格值? - Why am I getting a value with spaces using LTRIM and RTRIM? 我正在寻找一种仅在表1的值发生变化的情况下将触发器插入第二个表的方法 - I am looking for a way for a trigger to insert into a second table only where the value in table 1 changes Laravel:使用pluck从数据库中获取单一值 - Laravel : Getting Single value from the database using pluck 为什么我得到OleDBException“没有给出一个或多个必需参数的值?” - Why am I getting OleDBException “No value given for one or more required parameters?” 使用PHP如何从数据库表中检索某个字段值,然后将该单个字段值输入到新表中 - Using PHP how do I retrieve a certain field value from a database table and then input that single field value into a new table 为什么我在所有其他变量都能正常工作的6个变量中只有1个得到未定义的变量? - Why am I getting an undefined variable for only 1 of 6 variables where all the others work? 为什么我没有得到我期望的价值? - Why am I not getting the value that I expect? 为什么会出现“输入参数值”? - Why am I getting “Enter Parameter Value”? 为什么在Matlab中获取单元格数组而不是表-SQL查询和数据库连接? - Why am I getting a Cell array and not a Table in Matlab - SQL query and database connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM