我负责View / View Model,而不是项目的Model。模型设置如下:
[Table("Activity")]
public class Activity
{
[Key]
public int ActivityId { get; set; }
public string OtherProperties { get; set; }
}
[Table("Class1")]
public class Class1
{
[Key]
public int Class1Id { get; set; }
public string Name { get; set; }
public ObservableCollection<Activity> Activities { get; set; }
}
[Table("Class2")]
public class Class2
{
[Key]
public int Class2Id { get; set; }
public string Name { get; set; }
public ObservableCollection<Activity> Activities { get; set; }
}
编辑
我正在尝试通过对Activity类进行调用来填充DataGrid。此网格在另一类中具有“名称”属性的列。 如何从两个类中获取“名称”列的列表(或类对象本身的列表)通过ViewModel从Activity类中获得?
WPF
<DataGrid ItemsSource="{Binding Activities}"
Grid.ColumnSpan="3"
Grid.Row="1"
HorizontalAlignment="Stretch"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Complete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.CompleteTaskCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BasePage}}}"
CommandParameter="{Binding ActivityId}"
Content="{StaticResource FontAwesomeCheckIcon}"
Foreground="Green"
Grid.Column="0"
HorizontalAlignment="Center"
Style="{StaticResource FontAwesomeIconButton}"
VerticalAlignment="Center"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding DueDate, StringFormat=MM/dd/yyyy, Converter={local:UtcToLocalDateTimeValueConverter}}" Header="Due Date"/>
<DataGridTextColumn Binding="{Binding IsComplete, Converter={local:BooleanToStringParameterValueConverter}, ConverterParameter=CompletedOpen}" Header="Status"/>
<DataGridTextColumn Binding="{Binding Subject}" Header="Subject"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/> ??????????
</DataGrid.Columns>
</DataGrid>
ViewModel
Activities= DB.GetAll<Activity>().Where(x => x.IsDeleted == false && x.AssignedTo == user);
GetAll方法
public IEnumerable<T> GetAll<T>() where T : EcoDevBase, new()
{
// if no items, returns an empty list
IEnumerable<T> entityList = _Context.Set<T>().ToList();
return entityList;
}
答案 0 :(得分:1)
我不确定您在一开始的设置是什么,但是可以使用LINQ通过多种方法来满足您的要求。例如:
var desiredActivity = Activity;
var class1Names = DbContext.Class1.Where(e => e.Activities.Contains(desiredActivity).Select(e => e.Name)
var class2Names = DbContext.Class2.Where(e => e.Activities.Contains(desiredActivity).Select(e => e.Name)
var listOfNames = new List<string>;
listOfNames.AddRange(class1Names);
listOfNames.AddRange(class2Names);
这可能不是最佳的解决方案,但是您没有提供太多有关要如何处理业务对象的信息。
编辑:
请向ViewModel引用所需的活动列表,现在您想知道哪个Class对象包含此活动,则可以执行以下操作:
var listOfNames = new List<string>;
foreach (var activity in Activities)
{
var class1Names = DbContext.Class1.Where(e => e.Activities.Contains(activity).Select(e => e.Name)
var class2Names = DbContext.Class2.Where(e => e.Activities.Contains(activity).Select(e => e.Name)
listOfNames.AddRange(class1Names);
listOfNames.AddRange(class2Names);
}
现在,您可以通过调用
从listOfNames中删除重复项var fixedListOfNames = new List<string>;
fixedListOfNames.AddRange(listOfNames.Distinct());
如果要获取对象列表,则可以执行以下操作:
var listOfClasses1 = new List<Class1>;
var listOfClasses2 = new List<Class2>;
foreach (var activity in Activities)
{
var class1Names = DbContext.Class1.Where(e => e.Activities.Contains(activity);
var class2Names = DbContext.Class2.Where(e => e.Activities.Contains(activity);
listOfClasses1.AddRange(class1Names);
listOfClasses2.AddRange(class2Names);
}
我不确定您的配置,但您可能还需要添加
using System.Data.Entity
和/或.Include(e => e.Activities)
进行查询。
最后,如果我是您,我将创建一个业务对象类,其中包含您的DataGrid所需的属性,以使其不适用于db对象。