hoofedear
hoofedear
CC#
Created by hoofedear on 12/18/2022 in #help
✅ I want to get duplicate items in my List (EF Core)
I am trying to get a list of Cards from my database, and I am able to successfully get a List of Guids, where there are duplicate values, but when I try to use an efficient .Where() using .Contains(), it only returns 1 of the Card that shows up multiple times in the list. Here is my code:
var deckList = await _context.DeckContents
.Where(i => i.DeckId.ToString() == deckId)
.Select(i => i.CardId)
.ToListAsync();

var cardList = await _context.Cards
.Where(i => deckList.Contains(i.Id))
.ToListAsync();
var deckList = await _context.DeckContents
.Where(i => i.DeckId.ToString() == deckId)
.Select(i => i.CardId)
.ToListAsync();

var cardList = await _context.Cards
.Where(i => deckList.Contains(i.Id))
.ToListAsync();
cardList is a List<Card>, and originally my code did a foreach loop over the List of Guids, but that caused a lot of DB queries, so I found that doing .Contains helps, and it works fine, except it doesn't add duplicates, when I want it to. Hope this makes sense! Thanks!
49 replies