[英]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.