namespace Automated_VotingSystem.Models
{
public class DisplayCandidatesList
{
public List<DisplayCandidates>? DisplayCandidateslist { get; set;}
}
}
namespace Automated_VotingSystem.Models
{
public partial class DisplayCandidates
{
public int VoterId { get; set; }
public int? CollegeId { get; set; }
public int? EventId { get; set; }
public int CandidateId { get; set; }
public string? Manifesto { get; set; }
public byte[]? Symbol { get; set; }
public string? Symbol_path { get; set; }
}
}
public List<DisplayCandidates> DisplayCandidateslist { get; set; }
[HttpGet("[action]/{GetAllCandidates}")]
public async Task<DisplayCandidatesList> DisplayAllCandidates()
{
var list_of_candidates = (from c in _context.CandidateTables
join ac in _context.ApprovedCandidateTables on c.CandidateId equals ac.CandidateId
join s in _context.SymbolTables on c.SymbolId equals s.SymbolId
where ac.Approval == true
select new
{
candidate_id = ac.CandidateId,
manifesto = c.Manifesto,
symbol_path = s.Symbol_path
}).ToList();
return list_of_candidates;
}
The 1st snippet is my class, 2nd snippet is my model and 3rd snippet is my Controller. While executing the code I am getting a error after returning list of candidates as
Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: int? candidate_id, string manifesto, string symbol_path>>' to 'Automated_VotingSystem.Models.DisplayCandidatesList'
CodePudding user response:
I am not sure what you trying to do here,But It seems ; You dont need "DisplayCandidatesList" class, you only need one "DisplayCandidates" class. If you use this class in another class as a form of list,use like that List instead of List? ,But here you dont need that I guess.
Your list method can return "Task<List>" like that, I suggest you to study topic Collections and Generics in C# And dont use that naming in Controllers actions check naming standarts https://restfulapi.net/resource-naming/
CodePudding user response:
Your the DisplayAllCandidates method is defined to return list of DisplayCandidates. Therefore, use explicit type DisplayCandidates instead of anonymous:
return await (from c in _context.CandidateTables
join ac in _context.ApprovedCandidateTables on c.CandidateId equals ac.CandidateId
join s in _context.SymbolTables on c.SymbolId equals s.SymbolId
where ac.Approval == true
select new DisplayCandidates
{
CandidateId = ac.CandidateId,
Manifesto = c.Manifesto,
Symbol_path = s.Symbol_path
}).ToListAsync();
