© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
34 replies
demndev

✅ How can I improve this code?

It seems that I should change the way tags are stored. Or should I not?

public async Task<GetNotesByTagsQueryResult> Handle(GetNotesByTagsQuery query, CancellationToken ct)
    {
        var notes = new List<GetNotesByTagsQueryResultItem>();
        
        foreach (var excludedTag in query.ExcludedTags)
        {
            foreach (var includedTag in query.IncludedTags)
            {
                notes = await _db.Notes
                    .Where(n => n.Tags.Contains(includedTag) && !n.Tags.Contains(excludedTag))
                    .ProjectToType<GetNotesByTagsQueryResultItem>()
                    .Distinct()
                    .ToListAsync(ct);
            }
        }

        return new GetNotesByTagsQueryResult { Notes = notes };
    }
public async Task<GetNotesByTagsQueryResult> Handle(GetNotesByTagsQuery query, CancellationToken ct)
    {
        var notes = new List<GetNotesByTagsQueryResultItem>();
        
        foreach (var excludedTag in query.ExcludedTags)
        {
            foreach (var includedTag in query.IncludedTags)
            {
                notes = await _db.Notes
                    .Where(n => n.Tags.Contains(includedTag) && !n.Tags.Contains(excludedTag))
                    .ProjectToType<GetNotesByTagsQueryResultItem>()
                    .Distinct()
                    .ToListAsync(ct);
            }
        }

        return new GetNotesByTagsQueryResult { Notes = notes };
    }
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

✅ how can i improve this code ?
C#CC# / help
2y ago
✅ How can I change this code
C#CC# / help
2y ago