如何从数据库的两个表中填充ObservableCollection?

时间:2019-02-15 07:02:58

标签: c# mysql entity-framework mvvm observablecollection

尝试使用实体框架从数据库填充ObservableCollection。一切都很好,直到我开始使用链接表。

我创建了DeviceCategory和DeviceComplexity模型,现在在WyeModel中,我尝试将它们集成到DeviceCategoryViewModel中。此外,在DeviceCategoryViewModel中,我指示了从数据库获取信息的请求,但遇到了问题。如何用此信息填写ObservableCollection?我尝试了不同的方法,但没有成功,只是变得更加困惑。

DeviceCategoriesViewModel

class DeviceCategoryViewModel
{
    TechDContext dc = new TechDContext();
    public int Device_category_id { get; set; }
    public string Device_category_name { get; set; }
    public int Device_complexity_id { get; set; }
    public string Device_complexity_name { get; set; }

    public static DeviceCategoryViewModel DeviceCaterogyVM(DeviceCategory deviceCategory, DeviceComplexity deviceComplexity)
    {

        return new DeviceCategoryViewModel
        {
            Device_category_id = deviceCategory.Device_category_id,
            Device_category_name = deviceCategory.Category_name,
            Device_complexity_id = deviceCategory.Device_complexity_id,
            Device_complexity_name = deviceComplexity.Device_complexity_name
        };
    }

    public void FillDeviceDategories()
    {
        var q = from cat in dc.DeviceCategories
                join com in dc.DeviceComplexities on cat.Device_complexity_id equals com.Device_complexity_id
                select new
                {
                    Device_category_id = cat.Device_category_id,
                    Category_name = cat.Category_name,
                    Device_complexity_id = com.Device_complexity_id,
                    Device_complexity_name = com.Device_complexity_name
                };
        items = q;
        deviceCategories = Convert(items);

    }
    public ObservableCollection<DeviceCategoryViewModel> 
    Convert(IEnumerable<object> original)
    {
        return new ObservableCollection<DeviceCategoryViewModel>(original.Cast<DeviceCategoryViewModel>());
    }

    private IEnumerable<object> items;
    public IEnumerable<object> Items
    {
        get
        {
            return items;
        }
    }
    private ObservableCollection<DeviceCategoryViewModel> deviceCategories;
    public ObservableCollection<DeviceCategoryViewModel> DeviceCategories
    {
        get
        {
            FillDeviceDategories();
            return deviceCategories;
        }
    }

DeviceCategory模型

[Table("device_categories")]
public class DeviceCategory
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Device_category_id { get; set; }
    public string Category_name { get; set; }
    //[ForeignKey]
    public int Device_complexity_id { get; set; }

    public DeviceCategory()
    {

    }
    public DeviceCategory(string name, int complexity_id)
    {
        Category_name = name;
        Device_complexity_id = complexity_id;
    }
}

DeviceCompexity模型

[Table("device_complexities")]
public class DeviceComplexity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Device_complexity_id { get; set; }
    public string Device_complexity_name { get; set; }

    public DeviceComplexity()
    {

    }
    public DeviceComplexity(string name)
    {
        Device_complexity_name = name;
    }
}

我现在在转换方法上出现错误

1 个答案:

答案 0 :(得分:0)

您将尝试在单独的ObservableCollection<DeviceCategoryViewModel>函数中将LINQ查询结果转换为Convert

为什么不直接将您的LINQ查询结果收集到ObservableCollection<DeviceCategoryViewModel>

就这样使用

var q = from cat in dc.DeviceCategories
                    join com in dc.DeviceComplexities on cat.Device_complexity_id equals com.Device_complexity_id
                    select new DeviceCategoryViewModel    // <= Note This Line
                    {
                        Device_category_id = cat.Device_category_id,
                        Category_name = cat.Category_name,
                        Device_complexity_id = com.Device_complexity_id,
                        Device_complexity_name = com.Device_complexity_name
                    };

deviceCategories =  new ObservableCollection<DeviceCategoryViewModel>(q);

或者,如果您想在列表之后获得结果,则只需使用q.ToList()

deviceCategories =  new ObservableCollection<DeviceCategoryViewModel>(q.ToList());