So I had this little controller that is working great. The only issue, is that the StateLocation
OrderId was out of order in the returned string.
[HttpGet("GetLocationsByLanguageId/{id}")]
public async Task<ActionResult<IEnumerable<LanguageList>>> GetLocationsByLanguageId(Guid id)
{
var choicesByLanguage = await _context.LanguageList.Where(q => q.LanguageId == id)
.Include(p => p.StateLocation)
.ToListAsync();
return choicesByLanguage;
}
So I went to the Microsoft Entity Framework site to read about ordering and found this:
https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager
So I added an OrderBy
clause like you see below:
[HttpGet("GetLocationsByLanguageId/{id}")]
public async Task<ActionResult<IEnumerable<LanguageList>>> GetLocationsByLanguageId(Guid id)
{
var choicesByLanguage = await _context.LanguageList.Where(q => q.LanguageId == id)
.Include(p => p.StateLocation
.OrderBy(p => p.OrderId))
.ToListAsync();
return choicesByLanguage;
}
I don't get any errors on build, but when I try to hit the site, I get this:
Expression of type 'System.Linq.IOrderedQueryable' cannot be used for return type 'System.Linq.IOrderedEnumerable
So I searched around for that found a few possible solutions here:
OrderBy and List vs. IOrderedEnumerable
But none of those worked, they just generated new errors or variations on of the original error.
I've had trouble finding any answers that help with this issue when using .Net Core 3. Most of the answers are for older versions.
Has anyone been able to solve this .Net Core Entity Framework 3.0?
Thanks!
Models:
public partial class LanguageList
{
public LanguageList()
{
StateLocation = new HashSet<StateLocation>();
}
public Guid LanguageId{ get; set; }
public string LanguageName { get; set; }
public virtual ICollection<StateLocation> StateLocation { get; set; }
}
public partial class StateLocation
{
public Guid StateId { get; set; }
public Guid LanguageId { get; set; }
public string StateName { get; set; }
public string StateCapital { get; set; }
public int OrderId { get; set; }
public virtual LanguageList Language { get; set; }
}