Home > Mobile >  Cannot implicitly convert type 'System.Collections.Generic.List<List>' to 'Syst
Cannot implicitly convert type 'System.Collections.Generic.List<List>' to 'Syst

Time:08-29

Please Help mvc Controler idk convert stuf i have no idea ! what is wrong in this code

public async Task<ActionResult> Index()
{
    DateTime StartDate = DateTime.Today.AddDays(-6);
    DateTime EndDate = DateTime.Today;
    List<Transaction>
        SelectedTransaction = await _context.Transaction.
        Include(x => x.Category)
        .Where(y => y.Date > StartDate && y.Date < EndDate)
        .ToListAsync();
    return View();
}

}

CodePudding user response:

Cannot implicitly convert type 'System.Collections.Generic.List<Expose_Tracker.Models.Transaction>' to 'System.Collections.Generic.List<System.Transactions.Transaction>

It's giving me this error

CodePudding user response:

The error message describes your problem clearly and unambiguously.

You have two Transaction types. One is defined in the Expose_Tracker.Models namespace and the second in the System.Transactionsnamespace.

Therefore when you are preparing the List<Transaction> it is necessary to convert from the System.Transactions.Transaction type to the <Expose_Tracker.Models.Transaction type.

I suppose it should be like below:

public async Task<ActionResult> Index()
{
    List<Transaction> selectedTransaction = 
            await _context.Transaction
                     .Include(x => x.Category)
                     .Where(y => y.Date > StartDate && y.Date < EndDate)
                     .Select( t => new Expose_Tracker.Models.Transaction
                         {
                           ... assign required properties 
                         })
                     .ToListAsync();

    // And probably this collection will be passed as data model to the    view
    return View(selectedTransaction); 
}

CodePudding user response:

Is not the good idea to use the name of net classes as a custom class name, you have a name space overlapping.Try

List<Expose_Tracker.Models.Transaction> selectedTransaction =  await _context.Transaction .... and so on

    // And probably 
    return View(selectedTransaction); 

or just use var

var selectedTransaction =  await _context.Transaction .... and so on
  • Related