I am trying to get information from a related many-to-many two tables to a column. There are two classes for accounting for mobile devices and their IMEI: CDevice and ImeiList.

public partial class CDevice { public CDevice() { this.ImeiList = new HashSet<ImeiList>(); } public int Id { get; set; } public string Model { get; set; } public string Title { get; set; } public virtual ICollection<ImeiList> ImeiList { get; set; } } public partial class ImeiList { public ImeiList() { this.CDevice = new HashSet<CDevice>(); } public int Id { get; set; } public long IMEI { get; set; } public virtual ICollection<CDevice> CDevice { get; set; } } 

There is a collection in the ViewModel:

 private static ObservableCollection<CDevice> _oCollDevice; public static ObservableCollection<CDevice> OCollDevice { get { if (_oCollDevice == null) _oCollDevice = new ObservableCollection<CDevice>(); return _oCollDevice; } } 

As well as two methods, one of which receives information from the database:

 public static List<CDevice> GetDeviceList() { using (DevicesEntities context = new DevicesEntities()) { context.Configuration.LazyLoadingEnabled = false; var deviceList = context.CDeviceSet. Include("ImeiList").ToList(); return deviceList; } } 

And the second one calls it and adds the resulting list to the collection:

 private void GetDevices() { var CdeviceResult = GetDeviceList(); OCollDevice.Clear(); foreach (var item in CdeviceResult) { OCollDevice.Add(CurrentDevice); } } 

With ViewModel via Datacontext there is a View in which there is a Datagrid

 <DataGrid ItemsSource="{Binding Path=OCollDevice}" AutoGenerateColumns="False" Width="auto" HorizontalScrollBarVisibility="Disabled" > <DataGrid.Columns> <DataGridTextColumn Header="Наименование (производитель)" Binding="{Binding Path=Title}"></DataGridTextColumn> <DataGridTextColumn Header="Модель" Binding="{Binding Path=Model}"></DataGridTextColumn> <DataGridTextColumn Header="IMEI" Binding="{Binding Path=ImeiList.IMEI, Mode=OneWay}"></DataGridTextColumn> </DataGrid.Columns> </DataGrid> 

Question: how to display information in this table, so that all IMEIs and their corresponding devices are displayed, and that all devices and the corresponding IMEIs are displayed. Now in the column "IMEI" is empty, although the collection takes into account the number of matching IMEI and through PROFILER you can see the LEFT OUTER JOIN, which displays the necessary information. How to pull in a table IMEI?

  • Well, logically, ICollection<ImeiList> does not contain the IMEI property, so nothing is displayed. In what form do you want to display this collection in a single table cell? - Andrey NOP
  • "Name (manufacturer)", "Model", "IMEI". That all possible options are displayed. - Yuri
  • And is it all inside the same cell? Can you sketch how it should look like? - Andrew NOP
  • These are three columns and three different cells. - Yuri
  • Ok, you have one name, one model and a whole bunch of IMEI in one copy of CDevice , how do you want to display them in one cell of the table? - Andrew NOP

1 answer 1

In VM, you will have to use flat instances, and when working with databases, convert them into hierarchical ones.

Write such a VM class (do not forget about the implementation of the INPC if it is needed):

 public class DeviceVM { public int DeviceId { get; set; } public string Model { get; set; } public string Title { get; set; } public int ImeiId { get; set; } public long Imei { get; set; } } 

In the master VM, store the DeviceVM collection and bind it to the XAML.

Converting a CDevice collection to a CDevice collection will look something like this:

 DeviceVMCollection = CDeviceCollection .SelectMany(d => d.ImeiList.Select(i => new DeviceVM { DeviceId = d.Id, Model = d.Model, Title = d.Title, ImeiId = i.Id, Imei = i.IMEI }));