在WPF和EF 6中,如何只显示类的字段的子集? [英] In WPF and EF 6, how can I display only a subset of the fields of a class?
问题描述
na WPF应用程序,它也使用Entity Framework 6,我想只显示一个类的字段的子集,但我不清楚如何执行此操作。
在应用程序的主窗口中,Window_Loaded方法包含以下代码,它为类Course的所有字段提供表格显示,但我只想加载并显示字段的子集。这是当前代码:
private void Window_Loaded(object sender,RoutedEventArgs e)
{
System.Windows.Data .CollectionViewSource courseViewSource =((System.Windows.Data.CollectionViewSource)(this.FindResource(" courseViewSource")));;
_context.Courses.Include(c => c.Author).Load();
courseViewSource.Source = _context.Courses.Local;
}
所以,我不想加载课程的字段,但是我在哪里放Select()?
此致,
Juan Dent
Juan Dent
这是使用Linq投影调用,您可以使用anaymous类型,只读,类型或读/写的自定义类型来设置要从Linq查询返回的对象。
https: //csharp-station.com/Tutorial/Linq/Lesson02
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-结构/匿名类型
n a WPF application that also uses Entity Framework 6, I want to display only a subset of the fields of a class, but it is not clear to me how to do this. In the main window of the application, the Window_Loaded method contains the following code that provides a table display for all the fields of the class Course but I want to just load and display a subset of the fields. This is the current code:
private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Data.CollectionViewSource courseViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("courseViewSource"))); _context.Courses.Include(c => c.Author).Load(); courseViewSource.Source = _context.Courses.Local; }So, I don't want to load fields of Courses, but where do I put the Select()?
Regards,
Juan Dent
Juan Dent
解决方案It's called using a Linq projection where you shape the object you want returned from the Linq query by using an anaymous type , read only, type or a custom type that is read/write.
https://csharp-station.com/Tutorial/Linq/Lesson02
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types
这篇关于在WPF和EF 6中,如何只显示类的字段的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!