How to construct a specific linq query? - c#

I have three tables, doctor, office and appointment. Office table has DoctorId as a foreign key, and Apponitment has a foreign key OfficeId. I want to fetch all the appointments that have OfficeId equal to Ids in the list of offices that have the same doctorId. Specifically, I don't know how to extract ids from the list of offices. Here is my code, I skipped some parts for brevity:
public class Appointment1 : BaseEntity
{
public int? Patient1Id { get; set; }
[ForeignKey("Patient1Id")]
public Patient1 Patient { get; set; }
public int Office1Id { get; set; }
[ForeignKey("Office1Id")]
public Office1 Office { get; set; }
[DataType(DataType.Date)]
public DateTime StartDateAndTimeOfAppointment { get; set; }
[DataType(DataType.Date)]
public DateTime EndDateAndTimeOfAppointment { get; set; }
public bool? Status { get; set; }
public string Remarks { get; set;}
}
public class Doctor1 : BaseEntity
{
public int ApplicationUserId { get; set; }
[ForeignKey("ApplicationUserId")]
public ApplicationUser ApplicationUser { get; set; }
public string Name { get; set; }
public string Resume { get; set; }
}
public class Office1 : BaseEntity
{
public int Doctor1Id { get; set; }
[ForeignKey("Doctor1Id")]
public Doctor1 Doctor { get; set; }
public decimal InitialExaminationFee { get; set; }
public decimal FollowUpExaminationFee { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
public async Task<List<Appointment1>> GetAppointmentsWithSearchingAndPaging(QueryParameters queryParameters,
int userId)
{
var doctor = await _context.Doctors1.Where(x => x.ApplicationUserId == userId)
.FirstOrDefaultAsync();
var office = await _context.Offices.Where(x => x.Doctor1Id == doctor.Id)
.FirstOrDefaultAsync();
IQueryable<Appointment1> appointment = _context.Appointments1.Include(x => x.Patient)
.Where(x => x.Office1Id == office.Id)
.AsQueryable().OrderBy(x => x.Id);
if (queryParameters.HasQuery())
{
appointment = appointment
.Where(x => x.Office.Street.Contains(queryParameters.Query));
}
appointment = appointment.Skip(queryParameters.PageCount * (queryParameters.Page - 1))
.Take(queryParameters.PageCount);
return await appointment.ToListAsync();
}
The problem is with office which gives firstordefaultasync, and should give list, because I want all the ids, but in the end I get only appointments that have one identical officeid as a foreign key...thanks in advance!

Here is the answer, I needed this part of code, my question was not precise so I appologize:
var offices = await _context.Offices.Where(x => x.Doctor1Id == doctor.Id)
.ToListAsync();
IEnumerable<int> ids = offices.Select(x => x.Id);
IQueryable<Appointment1> appointment = _context.Appointments1.Include(x => x.Patient)
.Where(x => ids.Contains(office.Id))
.AsQueryable().OrderBy(x => x.Id);

Related

Linq Group by relative entites

My scenario: Users will be able to create lists and add items to these lists. What I want to do is to find the items in the lists created by the users at most.
Item Entity
public class Item:BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
public decimal DiscountedPrice{ get; set; }
public virtual ICollection<ItemList> ItemLists { get; set; }
}
Item List Entity
public class ItemList:BaseEntity
{
public string Name { get; set; }
public string Description { get; set; }
public int UserId { get; set; }
public ICollection<Item> Items { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
}
User Entity
public class User:BaseEntity
{
public string Name { get; set; }
public string Surname { get; set; }
public string Gsm { get; set; }
public string Email { get; set; }
public virtual ICollection<ItemList> ItemLists{ get; set; }
}
my DTO
public class TopItemsForUsers
{
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
public string ItemId { get; set; }
public string UserId { get; set; }
public int Quantity { get; set; }
}
My Item repository
var query = _context.Items.Include(l => l.ItemLists)
.GroupBy(g => g.ItemLists)
.Select(z => new TopItemsInLists { ItemId = z.Key.ToString(), Quantity = z.Count() })
.OrderByDescending(z => z.Quantity)
.Take(10);
I want to get products that are very present in users' lists
Where am I doing wrong? If anyone has any other suggestions
Try this query. I hope I understand question correctly.
var query =
from u in _context.Users
from il in u.ItemLists
from i in il.Items
group i by new { UserId = u.Id, ItemId = i.Id } into g
select new TopItemsInLists
{
UserId = g.Key.UserId.ToString(),
ItemId = g.Key.ItemId.ToString(),
Quantity = g.Count()
};
query = query
.OrderByDescending(z => z.Quantity)
.Take(10);

EF Core Add() only adds the last element to an ICollection

I'm trying to add an element to an ICollection object in EF Core 5. But only the last element remains on the list.
As an example, When a student submits an answer, I want to add him to the Participants of the exam(A). It works as it should. But when the same person submits another exam(B). His records are lost from the previous exam(A).
Here is my code.
SubmitExam Method in ExamController.cs
[HttpPost("submit")]
public async Task<ActionResult<GetExamDTO>> SubmitExam(GetExamDTO getExamDTO)
{
var username = User.FindFirst(ClaimTypes.Name).Value;
var user = await _userManager.Users
.Where(u => u.UserName == username)
.Include(u => u.ParticipatedExams)
.ThenInclude(er => er.Exam)
.FirstOrDefaultAsync();
if (user == null)
return Unauthorized("Invalid User");
var exam = await _dbContext.Exams
.Where(e => e.Id == getExamDTO.Id)
.Include(e => e.Creator)
.Include(e => e.Participants)
.Include(exam => exam.Questions)
.ThenInclude(question => question.Options)
.AsSplitQuery()
.SingleOrDefaultAsync();
if (exam == null)
return BadRequest("Invalid Exam Id");
if (!exam.SubmissionEnabled)
return BadRequest("Exam is over. You are too late.");
double marksObtained = 0;
double negativeMarksObtained = 0;
for (int i = 0; i != getExamDTO.Questions.Count(); ++i)
{
var question = exam.Questions.ElementAt(i);
if (question.CorrectAnswerText == getExamDTO.Questions[i].ProvidedAnswer.Text)
{
marksObtained += question.Marks;
}
else
{
marksObtained -= exam.NegativeMarks;
negativeMarksObtained -= exam.NegativeMarks;
}
}
var participatedFromExam = exam.Participants.Where(u => u.Id == user.Id)
.SingleOrDefault();
var participatedFromUser = user.ParticipatedExams
.Where(er => er.ExamId == exam.Id)
.SingleOrDefault();
System.Console.WriteLine("Submit" + participatedFromExam);
if (participatedFromExam == null && participatedFromUser == null) // If user sits for the first time, count him as new.
{
exam.Participants.Add(user); // Participants gets empty for every exam except the last submitted one. It fried my brain.
++exam.Attendees;
if (await _dbContext.SaveChangesAsync() > 0)
System.Console.WriteLine("Added submission");
var result = new ExamResult
{
Exam = exam,
Score = marksObtained
};
user.ParticipatedExams.Add(result);
await _userManager.UpdateAsync(user);
}
var examDto = examToDto(exam);
examDto.Participated = true;
examDto.MarksObtained = marksObtained;
examDto.Questions = getExamDTO.Questions;
examDto.NewSubmission = true;
examDto.NegativeMarks = negativeMarksObtained;
return Ok(examDto);
}
EntityUser.cs
public class EntityUser : IdentityUser
{
public ICollection<ExamResult> ParticipatedExams { get; set; }
public DateTime? LastActive { get; set; } = DateTime.UtcNow;
}
Exam.cs
public class Exam
{
public string Title { get; set; }
public Guid Id { get; set; }
public int Attendees { get; set; } = 0;
public string CreatorId { get; set; }
public EntityUser Creator { get; set; }
public DateTime CreatedAt { get; set; }
public int Duration { get; set; }
public double TotalMarks { get; set; }
public double NegativeMarks { get; set; }
public bool SubmissionEnabled { get; set; }
public ICollection<Question> Questions { get; set; }
public ICollection<EntityUser> Participants { get; set; }
}
ExamResult.cs
public class ExamResult
{
public Guid Id { get; set; }
public Exam Exam { get; set; }
public Guid ExamId { get; set; }
public double Score { get; set; }
}
GetExamDTO.cs
public class GetExamDTO
{
public string Title { get; set; }
public Guid Id { get; set; }
public int Attendees { get; set; } = 0;
public string Subject { get; set; }
public string CreatorId { get; set; }
public string Creator { get; set; }
public DateTime CreatedAt { get; set; }
public int Duration { get; set; }
public double TotalMarks { get; set; }
public double MarksObtained { get; set; }
public double NegativeMarks { get; set; }
public bool SubmissionEnabled { get; set; }
public bool Participated { get; set; }
public bool NewSubmission { get; set; }
public List<QuestionDTO> Questions { get; set; }
public List<ParticipantDTO> Participants { get; set; }
}
I will appreciate any help. Thanks in Advance.
fix the Examresult class by adding UserId
public partial class ExamResult
{
public Guid Id { get; set; }
public int? UserId { get; set; }
public virtual EntityUser User { get; set; }
public Guid ExamId { get; set; }
public virtual Exam Exam { get; set; }
public double Score { get; set; }
}
public partial class EntityUser : IdentityUser
{
public virtual ICollection<ExamResult> ParticipatedExams { get; set; }
public DateTime? LastActive { get; set; } = DateTime.UtcNow;
}
you can try to add fluent api to your db context
public virtual DbSet<ExamResult> ExamResults { get; set; }
.....
modelBuilder.Entity<ExamResult>(entity =>
{
entity.HasOne(d => d.User)
.WithMany(p => p.ParticipatedExams)
.HasForeignKey(d => d.UserId);
});
and code
var result = new ExamResult
{
UserId=user.Id,
ExamId = exam.Id,
Score = marksObtained
};
_dbContext.ExamResults.Add(result)
await _dbContext.SaveChangesAsync();

Filter a list Using Linq C#

public class Orgs
{
public int Id { get; set; }
public DateTime OrgCreationTime { get; set; }
public string AppUserId { get; set; }
public virtual ICollection<OrgPersonRelationshipDomain> ManyOrgPersonRelationship { get; set; }
}
public class OrgPersonRelationshipDomain
{
//public int ID { get; set; }
public int OrgId { get; set; }
public virtual OrgDomain Org { get; set; }
public bool IsDeleted { get; set; }
}
var orgs = await _context.Org.Where(x => x.Id == request.Id).ToListAsync();
How to filter list where "orgs" should give result with IsDeleted!= true?
Result I am getting is only ManyOrgPersonRelationship item but I want Orgs properties included in it.
`
Try this :
var orgs = _context.Org.Where(x => x.AppUserId == userId && x.ManyOrgPersonRelationship.Any(x => x.IsDeleted != false)).ToList();

how to reach a class's property in LINQ

I am trying to find unread messages count with linq. I am counting if message's lastmodified date greater than user's LastViewedOn date. My ConversationMembers class has LastViewedOn property. But i couldn't find a way to reach users's LastViewedOn property..
.Count(x => x.ConversationMembers
.Where(y => y.MembershipId.Value == membershipId)
.Any(y => y.LastViewedOn == null)
||
x.ConversationMessages.OrderByDescending(message => message.LastModifiedOn)
.Any(z => z.LastModifiedOn > x.ConversationMembers
.Where(y => y.MembershipId.Value == membershipId)
.Select()
.LastViewedOn.Value
)); //here i m trying to reach LastViewedOn value but it not work
Please can you help me about how can i reach that value?
Here my ConversationMember class
public class ConversationMember
{
public Guid Id { get; set; }
public Guid ConversationId { get; set; }
public virtual Conversation Conversation { get; set; }
public Guid? MembershipId { get; set; }
public DateTime? LastViewedOn { get; set; }
}
here ConversationMessage class
public class ConversationMessage
{
public Guid Id { get; set; }
[Required]
public string Text { get; set; }
public Guid ConversationId { get; set; }
public virtual Conversation Conversation { get; set; }
public Guid? MembershipId { get; set; }
[Required]
[Column(TypeName = "datetime2")]
public DateTime LastModifiedOn { get; set; }
}
And here conversation class
public class Conversation
{
public Guid Id { get; set; }
public virtual ICollection<ConversationMember> ConversationMembers { get; set; }
public virtual ICollection<ConversationMessage> ConversationMessages { get; set; }
[Required]
public DateTime LastModifiedOn { get; set; }
}
Try changing your LINQ to this:
.Count(x => x.ConversationMembers.Where(y => y.MembershipId.Value == membershipId)
.Any(y => y.LastViewedOn == null) || x.ConversationMessages.OrderByDescending(message => message.LastModifiedOn)
.Any(z => z.LastModifiedOn > x.ConversationMembers.FirstOrDefault(y => y.MembershipId.Value == membershipId).LastViewedOn.Value));

Entity Framework Core and many to many relation

I try to migrate a small application to Entity Framework Core but I cant get the many to many relation to work.
First my Entities
public class Currency : Entity<int>, IMayHaveUser
{
public string Code { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
public virtual List<CountryCurrency> CountryCurrencies { get; set; }
public bool IsUserDefined => User != null;
[ForeignKey("UserId")]
public virtual User User { get; set; }
public long? UserId { get; set; }
}
public class Country : Entity<int>, IMayHaveUser
{
public string Iso2Code { get; set; }
public virtual ICollection<Era> Eras { get; set; }
public string Name { get; set; }
public virtual List<CountryCurrency> CountryCurrencies { get; set; }
[NotMapped]
public bool IsUserDefined => User != null;
[ForeignKey("UserId")]
public virtual User User { get; set; }
public long? UserId { get; set; }
}
public class CountryCurrency : Entity<Guid>
{
public int CountryId { get; set; }
public Country Country { get; set; }
public int CurrencyId { get; set; }
public Currency Currency { get; set; }
}
and my DbContext is
modelBuilder.Entity().HasKey(currency => new {
currency.CountryId, currency.CurrencyId });
modelBuilder.Entity()
.HasOne(pt => pt.Country)
.WithMany(p => p.CountryCurrencies)
.HasForeignKey(pt => pt.CountryId);
modelBuilder.Entity<CountryCurrency>()
.HasOne(pt => pt.Currency)
.WithMany(t => t.CountryCurrencies)
.HasForeignKey(pt => pt.CurrencyId);
now when I add a currency for example
Currency currency;
Country country;
CountryCurrency countryCurrency;
currency = new Currency();
currency.Id = i++;
currency.User = null;
currency.Code = "ETB";
currency.Name = "Ethiopian Birr";
currency.Symbol = "Br";
country =
this._context.Countries.FirstOrDefault(
country1 => country1.Iso2Code == "ET");
if (country != null)
{
currency.CountryCurrencies = new List<CountryCurrency>();
countryCurrency = new CountryCurrency();
countryCurrency.Country = country;
countryCurrency.Currency = currency;
currency.CountryCurrencies.Add(countryCurrency);
this.InitialCurrencies.Add(currency);
}
this._context.Currencies.Add(currency);
so when now I'm retrieve the data in my test I get this with this code
Country = context.Countries.Include(country => country.CountryCurrencies).First();
I can't get the currency the id is set but the property not...
You have also to include the currency entity, not just the join entity
Country = context.Countries
.Include(country => country.CountryCurrencies)
.ThenInclude(e => e.Currency)
.First();

Categories