Join tables and get data from both of them - asp.net

I have been trying to get data from the table I have joined to the main user table, the second table is to hold images. My current code posted below, only return the ImageID from the table when I want to be retrieving the ImagePath field, just to note this is a separate table as the user can add many images.
These are the models:
[Table("accountInfo")] // Table name
public class accountInfo
{
[Key]
public int AccountID { get; set; }
public int UserId { get; set; }
public int UserIdent { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<UserImages > UserImages { get; set; }
}
[Table("UserImages")] // Table name
public class UserImages
{
[Key]
public int ImageID { get; set; }
public int AccountID { get; set; }
public string ImagePath { get; set; }
public string ImageDesc { get; set; }
public int ProfileImage { get; set; }
}
Controller:
public ActionResult Index()
{
int id = (int)WebSecurity.CurrentUserId;
var users = db.AccountInformation.Include(c => c.UserImages).Where(c => c.UserId == id);
return View(users.ToList());
}
I am assuming I have gone wrong in the models set up. Can anyone help?

var a = db.AccountInformation.Include(c => c.UserImages.Select(x => x.AccountId)).Where(c => c.UserId == id);

Related

Translate LINQ to LAMBDA Entity Framework Core using INCLUDE (NOT JOIN)

How to call multiple entities using Include method (not Join method) in Entity Framework Core? I am trying to translate this LINQ query to EF Core 5 syntax, but I do not know how to call multiple entities and join them together using include method.
var reservations = from reservation in _dbContext.Reservations
join customer in _dbContext.Users on reservation.UserId equals customer.Id
join movie in _dbContext.Movies on reservation.MovieId equals movie.Id
select new
{
Id = reservation.Id,
ReservationTime = reservation.ReservationTime,
CustomerName = customer.Id,
MovieName = movie.Name
};
I tried using multiple include and select method, but do not know how to call multiple entities and join
Here are my models
public class Reservation
{
public int Id { get; set; }
public int Qty { get; set; }
public double Price { get; set; }
public string Phone { get; set; }
public DateTime ReservationTime { get; set; }
public int MovieId { get; set; }
public int UserId { get; set; }
}
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Language { get; set; }
public string Duration { get; set; }
public DateTime PlayingDate { get; set; }
public DateTime PlayingTime { get; set; }
public double TicketPrice { get; set; }
public double Rating { get; set; }
public string Genre { get; set; }
public string TrailorUrl { get; set; }
public string ImageUrl { get; set; }
[NotMapped]
public IFormFile Image { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Role { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Controller code:
var reservations = _dbContext.Reservations
.Include(r => r.Id)
.Include(c => c.User)
.Select(x => new { x.Id, x.ReservationTime, x.User, x.User.Name });
If add to Reservation navigation properties Movie and User, your query can be simplified. Include cannot be used with Select together, it is ignored by EF translator.
var reservations = _dbContext.Reservations
.Select(r => new
{
Id = r.Id,
ReservationTime = r.ReservationTime,
CustomerName = r.User.Id,
MovieName = r.Movie.Name
});

How to save data in many to many relation without duplicate in my Card table [Entity Framework]

I have a problem. I cannot save cards without duplicates.
I have 3 tables a Deck, Card table and a CardDeck table which is the junction table.
The goal is to store a deck in the database without the cards being duplicated.
Table Card
namespace MTG_Deck.Models
{
public class Card
{
[Key]
public int CardId { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public string SetName { get; set; }
public string Artist { get; set; }
public string ManaCost { get; set; }
public string Rarity { get; set; }
public string ImageUrl { get; set; }
public string Toughness { get; set; }
public string Power { get; set; }
public string id { get; set; }
public virtual ICollection<DeckCard> Decks { get; set; }
}
}
Table Deck
namespace MTG_Deck.Models
{
public class Deck
{
[Key]
public int DeckId { get; set; }
[Required]
public string Name { get; set; }
[DataType(DataType.Date)]
public DateTime CreateAt { get; set; }
public User User { get; set; }
public virtual ICollection<DeckCard> Cards { get; set; }
}
}
Table CardDeck
namespace MTG_Deck.Models
{
public class DeckCard
{
public int DeckID { get; set; }
public int CardID { get; set; }
public Deck Deck { get; set; }
public Card Card { get; set; }
}
}
CardDeckRequest contains the content of the request.
namespace MTG_Deck.Models
{
public class CardDeckRequest
{
[Required]
public int UserID { get; set; }
[Required]
public string Token { get; set; }
[Required]
public Deck Deck { get; set; }
[Required]
[MinLength(5, ErrorMessage = "The deck must contain 60 cards.")]
public List<Card> Cards { get; set; }
}
}
Controller
[HttpPost]
public IActionResult Add([FromBody] CardDeckRequest request)
{
User user = _context.User.Where(u => u.Token == request.Token).FirstOrDefault<User>();
if (user == null) {
var error = new string[] {"You have to be connected to create a deck!"};
BadRequest(new { errors = new { success = error } });
}
List<DeckCard> deckList = new List<DeckCard>();
if (ModelState.IsValid) {
request.Deck.User = user;
foreach (var item in request.Cards)
{
Card card = _context.Card.FirstOrDefault(c => c.Name == item.Name);
if (card == null) {
card = item;
Console.WriteLine("coucou");
}
DeckCard deckCard = new DeckCard {
Card = card,
Deck = request.Deck
};
deckList.Add(deckCard);
}
_context.DeckCard.AddRange(deckList);
_context.SaveChanges();
var error = new string[] {"Your deck has been successfully saved."};
return Ok(new { errors = new { success = error } });
}
return BadRequest(ModelState.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
));
}

Select Max() with GroupBy, using linq method syntax, EF

I have a bunch of classes with some data:
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime bDate { get; set; }
//One-to-one with course
public int CourseId { get; set; }
public virtual Course Course { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection <Student_Course> Student_Courses { get; set; }
public int TeacherId { get; set; }
public virtual Teacher Teacher { get; set; }
}
public class Grade
{
public int Id { get; set; }
public int Mark { get; set; }
//one-to-many with Student_Course
public int Student_CourseId { get; set; }
public Student_Course Student_Course { get; set; }
}
public class Student_Course
{
public int Id { get; set; }
//many-to-many with student
public int StudentId { get; set; }
public virtual Student Student { get; set; }
//many-to-many with course
public int CourseId { get; set; }
public virtual Course Course { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime bDate { get; set; }
//one-to-many with student_course
public virtual ICollection <Student_Course> Student_Courses { get; set; }
}
Now i trying to write a query - for everyone teacher(name) needs to output his best student(name) by student mark, and the best mark, from class Grades, my code:
public IActionResult Task9()
{
var task9 = Db.Grades.GroupBy(gr => gr.Student_Course.Course.Teacher.Name).Select(x => new Task9
{
NameOfSt = x.Select(st => st.Student_Course.Student.Name).FirstOrDefault(),//Needs to correct this line
NameOfTeac = x.Select(r => r.Student_Course.Course.Teacher.Name).FirstOrDefault(),
BestMark = x.Max(gr => gr.Mark)
//NameOfSt = Db.Students.FirstOrDefault(st => st.Id ==x.FirstOrDefault().Student_Course.StudentId).Name
});
return View(task9);
}
So that code output correct teacher name and best mark on the course from students. But the student name is actually wrong. How can i fix that? Thanks for any help.
Teac
BestMark
NameStud
You could use OrderByDescending by Mark before select student name:
var task9 = Db.Grades.GroupBy(gr => gr.Student_Course.Course.Teacher.Name).Select(x => new Task9
{
NameOfSt = x.OrderByDescending(st => st.Mark).Select(st => st.Student_Course.Student.Name).FirstOrDefault(),//Needs to correct this line
NameOfTeac = x.Select(r => r.Student_Course.Course.Teacher.Name).FirstOrDefault(),
BestMark = x.Max(gr => gr.Mark)
});
Within the GroupBy you need to use the value of the grouping (in this case x) to select the Grade object with the highest Grade. In the code below, I get the bestGrade object by ordering by the Mark in descending order and then taking the first entry and then I take the student, name and best mark from that object:
public IActionResult Task9()
{
var task9 = Db.Grades.GroupBy(gr => gr.Student_Course.Course.Teacher.Name).Select(x =>
{
var bestGrade = x.OrderByDescending(y => y.Mark).First();
return new Task9
{
NameOfSt = bestGrade.Student_Course.Student.Name,//Needs to correct this line
NameOfTeac = bestGrade.Student_Course.Course.Teacher.Name,
BestMark = bestGrade.Mark
};
});
return View(task9);
}

Entity Framework 6 - Null value in nested query with select

I am facing a problem in EF6. When I execute the query Select it return the value. But when I add Select it returns null.
The code is here:
The (W) is not null here...
var list = db.X.Include("Y").Include("Z.W")
.OrderBy(c => c.Id)
.Skip(pageSize * page)
.Take(pageSize)
.ToList();
Here, The W value is null...
var list = db.X.Include("Y").Include("Z.W")
.Select(a => new { a.Id, a.Z})
.OrderBy(c => c.Id)
.Skip(pageSize * page)
.Take(pageSize)
.ToList();
Please help :)
UPDATE 1
public class academy
{
public int Id { get; set; }
[StringLength(255)]
[Index(IsUnique = true)]
public string Name { get; set; }
public string Logo { get; set; }
[Required]
public Owner owner { get; set; }
public List<location> Location { get; set; }
}
public class location
{
public int Id { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string Neighborhood { get; set; }
public string Street { get; set; }
public academy Academy { get; set; }
public List<stadium> Stadiums { get; set; }
public List<Administrators> Administrators { get; set; }
public List<addition> Addition { get; set; }
public List<Pricing> Pricing { get; set; }
public List<time_frame> TimeFrames { get; set; }
[NotMapped]
public string Details {
get { return (City + " - " + Street); }
}
}
public class Pricing
{
public int Id { get; set; }
public double Price { get; set; }
public double? PriceAfterOffer { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public location Location { get; set; }
public players_capacity StadiumCapacity { get; set; }
}
public class players_capacity
{
public int Id { get; set; }
[StringLength(255)]
[Index(IsUnique = true)]
public string Capacity { get; set; }
}
var list = db.locations
.Select(a => new { a.Id, a.City, a.Region, a.Street, a.Latitude, a.Longitude, a.Pricing, a.Academy })
.OrderBy(c => c.Id)
.Skip(pageSize * page)
.Take(pageSize)
.ToList();
The problem is on players_capacity always null
Any additional data specified by Include is ignored if the query changes "shape", in this case your additional .Select expression invalidates the previous Include terms so they are ignored. The same happens if you do a GroupBy or GroupJoin.
Fortunately the fix is simple: explicitly specify the Y and Z.W members in your projection:
var list = db.X
.Select( x => new { x.Id, x.Z, x.Y, x.Z.W } )
.OrderBy( p => p.Id )
.Skip( () => pageSize * page )
.Take( () => pageSize )
.ToList();
(Note that I'm using the Expression<> overloads of Skip and Take, as those are better for use with EF).

ASP.NET MVC 4 Code First Many to Many Adding to Collection

I am using ASP.NET MVC 4 code first pattern for database layer. I have a many to many relationship between UserProfile and Task. When I try to add a task to the the collection of tasks of a user, it's added but if I try to query it and see if it's there it's not showing up.
My model:
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string SirName { get; set; }
public string Position { get; set; }
public string Email { get; set; }
public ICollection<TaskModels> Tasks {get; set; }
public bool? isActive { get; set; }
public UserProfile()
{
Tasks = new HashSet<TaskModels>();
}
}
public class TaskModels
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public ICollection<UserProfile> Employees { get; set; }
public int TimeNeeded { get; set; }
public int TimeWorked { get; set; }
public string Status { get; set; }
public bool isActive { get; set; }
public TaskModels()
{
Employees = new HashSet<UserProfile>();
}
}
public class WorkLogModels
{
public int Id { get; set; }
public UserProfile Author { get; set; }
public DateTime TimeBeganWorking { get; set; }
public int TimeWorkedOn { get; set; }
public TaskModels Task { get; set; }
public string Description { get; set; }
}
public class TimeTrackerDb : DbContext
{
public TimeTrackerDb() : base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<TaskModels> Tasks { get; set; }
public DbSet<WorkLogModels> WorkLogs { get; set; }
}
I try to check if a UserProfile already exists in a Task's Employees list and it's always empty.
[HttpPost]
public ActionResult Create(WorkLogModels worklogmodels)
{
var tasks = db.Tasks.Where(x => x.Name == worklogmodels.Task.Name).SingleOrDefault();
if (tasks == null)
{
return View(worklogmodels);
}
if (ModelState.IsValid)
{
var user = db.UserProfiles.Where(x => x.UserId == WebSecurity.CurrentUserId).FirstOrDefault();
var task = db.Tasks.Where(x => x.Name == worklogmodels.Task.Name).FirstOrDefault();
WorkLogModels log = new WorkLogModels();
log.Description = worklogmodels.Description;
log.TimeBeganWorking = worklogmodels.TimeBeganWorking;
log.TimeWorkedOn = worklogmodels.TimeWorkedOn;
log.Author = user;
log.Task = task;
db.WorkLogs.Add(log);
if (!db.UserProfiles.Where(x => x.UserId == WebSecurity.CurrentUserId).First().Tasks.Any(x=> x.Name == worklogmodels.Task.Name))
{
db.UserProfiles.Where(x => x.UserId == WebSecurity.CurrentUserId).FirstOrDefault().Tasks.Add(task);
db.Tasks.Where(x => x.Name == worklogmodels.Task.Name).FirstOrDefault().Employees.Add(user);
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(worklogmodels);
}
I've been fighting with this for two days now.
Any help will be greatly appreciated
EDIT:
I am not sure if I made myself clear. In the Crate action for the WorkLog Controller I am trying to put the current user in the current task's collection and vice versa. It works correctly the first time, but then if I do it again it fails to skip the if statement and tries to add it once again and throws an exception : System.Data.SqlClient.SqlException. It's trying to add the same record to the intermediate table.

Resources