I can't seem to understand why I have errors with my method at the very bottom.
I have these two methods below that work fine.
public async Task<Claim> GetClaim(string id)
{
var query = _context.Claims.AsQueryable();
var claim = await query.FirstOrDefaultAsync(c => c.ClaimNumber == id);
return claim;
}
public async Task<IEnumerable<Message>> GetMessageThread(int userId, int recipientId)
{
var messages = await _context.Messages
.Include(u => u.Sender).ThenInclude(p => p.Photos)
.Include(u => u.Recipient).ThenInclude(p => p.Photos)
.Where(m => m.RecipientId == userId && m.RecipientDeleted == false
&& m.SenderId == recipientId
|| m.RecipientId == recipientId && m.SenderId == userId
&& m.SenderDeleted == false)
.OrderByDescending(m => m.MessageSent)
.ToListAsync();
return messages;
}
But with this method I get two red errors under the first line and last for
'DbSet' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'DbSet' could be found
and
Task> results Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists
Here is the method
public async Task<IEnumerable<Claim>> GetClaims(GetClaimsDto claimParams)
{
var claims = await _context.Claims;
if (claimParams.MatterNumber != null) {
claims = claims.Where(c => c.MatterNumber == claimParams.MatterNumber);
}
if (claimParams.FirstName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.FirstName, "%" + claimParams.FirstName + "%"));
}
if (claimParams.LastName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.LastName, "%" + claimParams.LastName + "%"));
}
var results = claims.OrderByDescending(m => m.MatterNumber).ToListAsync();
return results;
}
Here is my Claims entity:
public class Claim
{
public int Id { get; set; }
public string ClaimNumber { get; set; }
public string MatterNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Data { get; set; }
[NotMapped]
public string[] Errors { get; set; }
[NotMapped]
public List<string> SearchResults { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime EditedDate { get; set; }
public string EditedBy { get; set; }
}
Here are the errors
_context.Claims
is not async therefore you do not need to await it.
.ToListAsync()
is async therefore you do need to await it.
Try this instead.
public async Task<IEnumerable<Claim>> GetClaims(GetClaimsDto claimParams)
{
var claims = _context.Claims;
if (claimParams.MatterNumber != null) {
claims = claims.Where(c => c.MatterNumber == claimParams.MatterNumber);
}
if (claimParams.FirstName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.FirstName, "%" + claimParams.FirstName + "%"));
}
if (claimParams.LastName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.LastName, "%" + claimParams.LastName + "%"));
}
var results = await claims.OrderByDescending(m => m.MatterNumber).ToListAsync();
return results;
}