C
C#8mo ago
Bourbon

I don't know how to get information from several tables

There are Users and Passports tables. Information is output from the Users table, but it is not output from the Passports table. What can be done? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace StudyGroupDiary { /// <summary> /// Логика взаимодействия для PersonalBusinessPage.xaml /// </summary> public partial class PersonalBusinessPage : Page { private Users _currentUsers = new Users(); public PersonalBusinessPage(Users selectedUsers) { InitializeComponent(); if (selectedUsers != null) { _currentUsers = selectedUsers; } DataContext = _currentUsers; } private void BtnSave_Click(object sender, RoutedEventArgs e) { StringBuilder errors = new StringBuilder(); if (string.IsNullOrWhiteSpace(_currentUsers.Name)) errors.AppendLine("Укажите Имя!"); if (string.IsNullOrWhiteSpace(_currentUsers.Surname)) errors.AppendLine("Укажите Фамилию!"); if (string.IsNullOrWhiteSpace(_currentUsers.MiddleName)) errors.AppendLine("Укажите Отчество!"); if (string.IsNullOrWhiteSpace(_currentUsers.Role)) errors.AppendLine("Укажите Роль!"); if (string.IsNullOrWhiteSpace(_currentUsers.DateOfBirth)) errors.AppendLine("Укажите дату рождения!"); if (string.IsNullOrWhiteSpace(_currentUsers.Gender)) errors.AppendLine("Укажите пол!"); if (errors.Length > 0) { MessageBox.Show(errors.ToString()); return; } if (_currentUsers.UserID == 0) StudyGroupDiaryBDEntities.GetContext().Users.Add(_currentUsers); try { StudyGroupDiaryBDEntities.GetContext().SaveChanges(); MessageBox.Show("Информация сохранена!"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } }
4 Replies
Angius
Angius8mo ago
I don't see you getting any data from the database in this code That said, if you want to get all users and their passport information, for example, it would be best to use a .Select() into a DTO Second best option would be an .Include() That is all assuming you have proper relationship between users and passports set up, of course
Bourbon
BourbonOP8mo ago
Thank you very much for the advice. And how can this be done? This is the first time I've heard of .Select() and .Include()
Angius
Angius8mo ago
Let's take database models like this:
public class Blogpost
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public List<Tag> Tags { get; set; }
public User Author { get; set; }
public id AuthorId { get; set; }
public DateTime CreatedAt { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public string Description { get; set; }
public List<Blogpost> Blogposts { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
public DateTime CreatedAt { get; set; }
public List<Blogpost> Blogposts { get; set; }
}
public class Blogpost
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public List<Tag> Tags { get; set; }
public User Author { get; set; }
public id AuthorId { get; set; }
public DateTime CreatedAt { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public string Description { get; set; }
public List<Blogpost> Blogposts { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
public DateTime CreatedAt { get; set; }
public List<Blogpost> Blogposts { get; set; }
}
We would like to get a list of five most recent blogposts, with their tags and authors. We don't need all of the information about the tags or authors, mind you. We don't need the author's password, for example. Thus, we create a DTO that will represent the shape of data we actually do want:
public class BlpogpostDto
{
public int Id { get; init; }
public string Title { get; init; }
public string Excerpt { get; init; }
public DateTime CreatedAt { get; init; }
public List<TagDto> Tags { get; init; }
public string AuthorName { get; init; }
}
public class TagDto
{
public string Name { get; init; }
public string Color { get; init; }
}
public class BlpogpostDto
{
public int Id { get; init; }
public string Title { get; init; }
public string Excerpt { get; init; }
public DateTime CreatedAt { get; init; }
public List<TagDto> Tags { get; init; }
public string AuthorName { get; init; }
}
public class TagDto
{
public string Name { get; init; }
public string Color { get; init; }
}
and we can use .Select() to project our database models into that DTO, so the database only selects what we actually need. That is, it does not execute a SELECT * FROM but rather SELECT a, b, c FROM
var blogpost = await _context.Blogposts
.OrderByDescending(b => b.CreatedAt) // sort the blogposts
.Select(b => new BlogpostDto // select the data
Id = b.Id,
Title = b.Title,
Excerpt = $"{b.Body[..400]}...",
CreatedAt = CreatedAt,
AuthorName = b.Author.Name, // use the nav property
Tags = b.Tags.Select(t => new TagDto { // select tags
Name = t.Name,
Color = t.Color
})
)
.Take(5) // get 5 of them
.ToListAsync(); // execute the query
var blogpost = await _context.Blogposts
.OrderByDescending(b => b.CreatedAt) // sort the blogposts
.Select(b => new BlogpostDto // select the data
Id = b.Id,
Title = b.Title,
Excerpt = $"{b.Body[..400]}...",
CreatedAt = CreatedAt,
AuthorName = b.Author.Name, // use the nav property
Tags = b.Tags.Select(t => new TagDto { // select tags
Name = t.Name,
Color = t.Color
})
)
.Take(5) // get 5 of them
.ToListAsync(); // execute the query
Bourbon
BourbonOP8mo ago
Thank you very much❤️
Want results from more Discord servers?
Add your server