var eventTickets = await queryBus.Send(new GetTicketsByEventIdQuery(new List<int> { id…
- User
var eventTickets = await _queryBus.Send(new GetTicketsByEventIdQuery(new List { id }));
public class GetTicketsByEventIdQuery : ICacheLookupQuery<int, TicketDto> { public IEnumerable Ids { get; set; }
public GetTicketsByEventIdQuery(IEnumerable<int> ids) { Ids = ids; }}
public class GetTicketsByEventIdQueryHandler : IQueryHandler<GetTicketsByEventIdQuery, ILookup<int, TicketDto>> { private readonly IDbConnectionFactory _dbConnectionFactory;
public GetTicketsByEventIdQueryHandler(IDbConnectionFactory dbConnectionFactory) { _dbConnectionFactory = dbConnectionFactory; } public async Task<ILookup<int, TicketDto>> Handle( GetTicketsByEventIdQuery request, CancellationToken cancellationToken ) { var tickets = await _dbConnectionFactory.ExecuteQuery<IEnumerable<TicketDto>>( db => db.Query("shpTicket").Select("*").WhereIn("EventID", request.Ids).GetAsync<TicketDto>(), cancellationToken ); return tickets.ToLookup(t => t.EventId, t => t); }} }
Collection initialization can be simplifiedIDE0028 List.List() (+ 2 overloads) Initializes a new instance of the List class that is empty and has the default initial capacity.
is there a better way to write this?
- Assistant
Yes, you can simplify the collection initialization. The message "Collection initialization can be simplified IDE0028" suggests that you're creating a collection in a way that can be made more concise.
In your code, you're creating a
List<int>with a single item:new List<int> { id }Since the
GetTicketsByEventIdQueryconstructor accepts anIEnumerable<int>, and you only need to pass a collection with a single item, you can use an array initialization instead:new[] { id }Arrays in C# implement , so this will work seamlessly with your constructor. Here's the simplified code: