© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
48 replies
hoofedear

✅ 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
cardList
is a
List<Card>
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!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ EF Core doesn't stop tracking list items.
C#CC# / help
15mo ago
❔ EF Core Person Task List
C#CC# / help
3y ago
EF Core
C#CC# / help
2y ago