C# 派生类上的ObservableCollection
我是C#的初学者(来自SQL背景),所以这个问题中的一些术语可能不正确。请提前道歉 我正在尝试使用C#/WPF/SQLite创建一个食品消费跟踪应用程序(允许您跟踪您吃的食物、计算卡路里等)。 我已经使用EF中的添加模型向导创建了数据库块和实体模型 我有一个ConsumptionLog实体,就像这样C# 派生类上的ObservableCollection,c#,wpf,entity-framework,class,observablecollection,C#,Wpf,Entity Framework,Class,Observablecollection,我是C#的初学者(来自SQL背景),所以这个问题中的一些术语可能不正确。请提前道歉 我正在尝试使用C#/WPF/SQLite创建一个食品消费跟踪应用程序(允许您跟踪您吃的食物、计算卡路里等)。 我已经使用EF中的添加模型向导创建了数据库块和实体模型 我有一个ConsumptionLog实体,就像这样 public partial class ConsumptionLog { public int consumptionID { get; set; } public Nullabl
public partial class ConsumptionLog
{
public int consumptionID { get; set; }
public Nullable<int> dayMealNumber { get; set; }
public Nullable<int> foodID { get; set; }
public Nullable<int> mealID { get; set; }
public Nullable<decimal> servings { get; set; }
public System.DateTime logDate { get; set; }
public virtual Meal Meal { get; set; }
public virtual Food Food { get; set; }
}
公共部分类消耗日志
{
公共int消费ID{get;set;}
公共可空dayMealNumber{get;set;}
公共可空foodID{get;set;}
公共可空mealID{get;set;}
公共可空服务{get;set;}
public System.DateTime logDate{get;set;}
公共虚拟用餐{get;set;}
公共虚拟食品{get;set;}
}
及
公共部分类食品
{
公共食品
{
this.ConsumptionLogs=new HashSet();
this.MealContents=newhashset();
}
公共int{get;set;}
公共字符串存储{get;set;}
公共字符串brandName{get;set;}
公共字符串foodName{get;set;}
公共字符串foodUnit{get;set;}
公共可空foodNoOfUnits{get;set;}
公共可空卡路里{get;set;}
公共可空carb{get;set;}
公共可为空的sugar{get;set;}
公共可空蛋白质{get;set;}
公共可空fat{get;set;}
公共可空光纤{get;set;}
公共可为空的gms{get;set;}
公共可空ml{get;set;}
公共短isActive{get;set;}
公共虚拟ICollection度量内容{get;set;}
}
上面的代码是自动生成的。我试图在另一个类中扩展ComsumptionLog,以添加更多属性。例如,这是一个片段
public partial class ConsumptionLogView : ConsumptionLog
{
public ObservableCollection<ConsumptionLogView> CLViewList { get; set; }
string _FoodName;
public string FoodName
{
get
{
if (Food == null)
{
return "Total";
}
else
{
return Food.foodName.ToString() + " : "
+ Convert.ToDouble(Food.foodNoOfUnits).ToString() + " " + Food.foodUnit.ToString();
}
}
set
{
}
}
public decimal _Calories;
public decimal Calories
{
get
{
if (Food == null)
{
return _Calories;
}
else
{
return Convert.ToDecimal(Food.calories);
}
}
set
{
}
}
}
公共部分类ConsumptionLogView:ConsumptionLog
{
公共ObservableCollection CLViewList{get;set;}
字符串_FoodName;
公共字符串FoodName
{
得到
{
如果(食物==null)
{
返回“总计”;
}
其他的
{
return Food.foodName.ToString()+“:”
+Convert.ToDouble(Food.foodNoOfUnits.ToString()+“”+Food.foodUnit.ToString();
}
}
设置
{
}
}
公共十进制热量;
公共十进制卡路里
{
得到
{
如果(食物==null)
{
返回热量;
}
其他的
{
返回换算到特定值(食物、卡路里);
}
}
设置
{
}
}
}
目的是,我可以用这样的东西
private void LoadConsumptionLogs()
{
FitnessLogDBEntities ctx = new FitnessLogDBEntities();
var consumptionList = (
from s in ctx.ConsumptionLogs
where s.logDate == LogDate.SelectedDate.Value
select s).ToList<ConsumptionLog>();
ObservableCollection<ConsumptionLogView> conLogViewColl =
(ObservableCollection<ConsumptionLogView>)consumptionList.Cast<ObservableCollection<ConsumptionLogView>>();
var consumptionItem = new ConsumptionLogView();
consumptionItem._Calories = GetTotalCals(conLogViewColl);
conLogViewColl.Add(consumptionItem);
ConsumptionLog.ItemsSource = conLogViewColl;
}
private decimal GetTotalCals(ObservableCollection<ConsumptionLogView> cons)
{
double Total = 0;
foreach (ConsumptionLogView con in cons)
{
Total += Convert.ToDouble(con.Calories);
}
return Convert.ToDecimal(Total);
}
private void LoadConsumptionLogs()
{
FitnessLogDBEntities ctx=新FitnessLogDBEntities();
var消耗列表=(
从ctx.ConsumptionLogs中的s
其中s.logDate==logDate.SelectedDate.Value
选择s.ToList();
ObservableCollection conLogViewColl=
(ObservableCollection)consumptionList.Cast();
var consumptionItem=新的ConsumptionLogView();
消耗项目_卡路里=总热量(conLogViewColl);
conLogViewColl.Add(消耗项);
ConsumptionLog.ItemsSource=conLogViewColl;
}
私有十进制GetTotalCals(ObservableCollection cons)
{
双倍合计=0;
foreach(ConsumptionLogView con in cons)
{
总+=换算成双倍(卡路里);
}
返回转换为特定值(总计);
}
显然,这不起作用,并且在我试图将基类ObservableCollection(ConsumptionLog)强制转换为派生类(ConsumptionLogView)时出错
我的问题是:
不必使用继承来扩展类。在C#中扩展类的第二种方法是使用分部类。这意味着一个类由多个文件组成。通常,自动生成的类已经是部分的。这意味着扩展类非常简单。假设代码生成器创建了ComsumptionLog.cs(注意
partial
标识符)
您只需创建第二个文件,如comsumptionogextension.cs
public partial class ConsumptionLog
{
//Additional properties and methods
}
使用您需要的所有附加属性和方法。有关更多信息,请查看。这样,您就不需要强制转换任何类型,因为类仍然是相同的。如果将扩展名文件保存在与生成的文件不同的文件夹中,则代码生成器在生成运行期间不应触摸它。请注意,名称空间必须相同
我认为这种方法应该以一种非常干净和简单的方式解决您的问题。
ConsumptionLog
是一个局部类,您也可以扩展simple。您是否考虑过扩展现有类而不是使用i
public partial class ConsumptionLog
{
//Some properties
}
public partial class ConsumptionLog
{
//Additional properties and methods
}