Demeteor
Demeteor

Reputation: 1223

ASP.NET Core getting data list via ID

Edit: Ok Ive just noticed this. each time I try to add the ID of the user inside the table it always adds a new ID. maybe that is the reason I am getting 0 in count.

I am trying to get all the data from a table with userID in it. Example users favorite movies.

I retrieve the ID from my database and trying to do the following.

What am I doing wrong? There are items in the table Favourites with the ID of that user. but I still get count 0.

 var  item = _db.Favorites.Where(s => s.Userid.Equals(userId)).ToList();        

 // Here the (userID) has userID in it, but var item has count 0 in it. 
 // So I can never access my foreach loop 
 foreach (var movie in item)
 {
     ....
 }

Upvotes: 1

Views: 1969

Answers (1)

Wasim
Wasim

Reputation: 724

If UserId is Integer

if(userId != null && userId > 0)
 {
   var  item = _db.Favorites.Where(s => s.Userid == userId).ToList();
 } 

If UserId is String

if(userId != null)
  {
    var  item = _db.Favorites.Where(s => s.Userid.ToLower() == userId.ToLower()).ToList();
  } 

OR

if(userId != null)
      {
        var  item = _db.Favorites.Where(s => String.Equals(s.Userid.ToLower() ,userId.ToLower()).ToList();
      } 

 

Upvotes: 1

Related Questions