I am not even sure how to word this question so bear with me please. I will edit if needed.
我甚至不确定怎么说这个问题所以请耐心等待。如果需要我会编辑。
Lets start with my code:
让我们从我的代码开始:
var assetMonth = "assest."+Month;
var billableAssests = (from s in db.Assets
where s.SystemPosition == 1
select s).ToList();
try {
foreach (var assests in billableAssests)
{
assests.PreviousDateBilled = assests.LastDateBilled;
assests.LastDateBilled = today;
assetMonth = assests.MonthlyChargeRate; <===HERE
assests.RunnungLeaseTotal = assests.RunnungLeaseTotal + assests.MonthlyChargeRate;
if (assests.MonthBilled == null)
{
assests.MonthBilled = 1;
}
else
{
assests.MonthBilled = assests.MonthBilled + 1;
}
//db.Entry(billableAssests).State = EntityState.Modified;
db.SaveChanges();
}
}
I am getting the month from the user that they want to bill and want to insert the monthly charge rate into that months column. I cannot figure out how to do this for the life of me. Any suggestions or tell me where i can start looking?
我从用户那里获得他们想要开帐单的月份,并希望将月费率插入该月份列。我无法弄清楚如何为我的生活做这件事。有什么建议或告诉我在哪里可以开始寻找?
Thanks!
谢谢!
EDIT*** Model for table
编辑***表的模型
public class Asset
{
[Key]
public string AssetTag { get; set; }
public string SerialNumber { get; set; }
public int WarrantyPeriod { get; set; }
public string DeviceApprover { get; set; }
public int Dept_id { get; set; }
public string AssetLocation { get; set; }
public string DeliveryLocation { get; set; }
public int SectionKey { get; set; }
public int VendorKey { get; set; }
public int DeviceInformationKey { get; set; }
public int EmployeeKey { get; set; }
public decimal PurchasePrice { get; set; }
public decimal? BaseLeaseRate { get; set; }
public decimal? MonthlyChargeRate { get; set; }
public decimal? LeaseTotal { get; set; }
public int? MonthBilled { get; set; }
public DateTime? LeaseStartDate { get; set; }
public DateTime? LeaseEndDate { get; set; }
public decimal? RunnungLeaseTotal { get; set; }
public int deliveryEmployeeKey { get; set; }
public DateTime? InstallDate { get; set; }
public Boolean? Confirm { get; set; }
public string Status { get; set; }
public int? SystemPosition { get; set; }
public DateTime? LastDateBilled { get; set; }
public DateTime? PreviousDateBilled { get; set; }
public DateTime? ReturnedDate { get; set; }
public int Account { get; set; }
public decimal? Jan { get; set; }
public decimal? Feb { get; set; }
public decimal? Mar { get; set; }
public decimal? Apr { get; set; }
public decimal? May { get; set; }
public decimal? June { get; set; }
public decimal? July { get; set; }
public decimal? Aug { get; set; }
public decimal? Sept { get; set; }
public decimal? Oct { get; set; }
public decimal? Nov { get; set; }
public decimal? Dec { get; set; }
public virtual Section Section { get; set; }
public virtual Vendor Vendor { get; set; }
public virtual DeviceInformation DeviceInformation { get; set; }
public virtual Employee Employee { get; set; }
1 个解决方案
#1
2
Well, you have a couple of options.
好吧,你有几个选择。
The first is to refactor your model and possibly your database, so that rather than having 12 fields for 12 months, you have a collection of month values. That would be easier to update, since you could just use the passed-in month to figure out which entry you want, if it exists, or add it if it doesn't.
第一个是重构你的模型,可能还有你的数据库,这样你就可以获得12个月的12个字段。这将更容易更新,因为您可以使用传入的月份来确定您想要的条目(如果存在),或者如果不存在则添加它。
public class AssetMonth
{
string AssetTag { get;set;}
DateTime Month { get;set;} // DateTime would *probably* be the best choice for this
Decimal? MonthlyChargeRate { get;set;}
}
The second is to make use of a good old-fashioned switch
statement:
第二个是利用一个很好的老式开关声明:
switch(Month) // I don't know what type this is; I'm assuming string.
{
case "Jan":
model.Jan = assets.MonthlyChargeRate;
break;
case "Feb":
model.Feb = assets.MonthlyChargeRate;
// etc.
}
Third, if you're really feelin' fancy, you could use reflection to set the value.
第三,如果你真的感觉很奇特,你可以用反射来设定价值。
PropertyInfo prop = typeof(Asset).GetProperty(Month); // again, assuming string
prop.SetValue(model, assets.MonthlyChargeRage);
But I would encourage you to refactor the model. That will give you the best design, and remove the need for workarounds like options 2 and 3.
但我鼓励你重构模型。这将为您提供最佳设计,并消除对选项2和3等变通方法的需求。
#1
2
Well, you have a couple of options.
好吧,你有几个选择。
The first is to refactor your model and possibly your database, so that rather than having 12 fields for 12 months, you have a collection of month values. That would be easier to update, since you could just use the passed-in month to figure out which entry you want, if it exists, or add it if it doesn't.
第一个是重构你的模型,可能还有你的数据库,这样你就可以获得12个月的12个字段。这将更容易更新,因为您可以使用传入的月份来确定您想要的条目(如果存在),或者如果不存在则添加它。
public class AssetMonth
{
string AssetTag { get;set;}
DateTime Month { get;set;} // DateTime would *probably* be the best choice for this
Decimal? MonthlyChargeRate { get;set;}
}
The second is to make use of a good old-fashioned switch
statement:
第二个是利用一个很好的老式开关声明:
switch(Month) // I don't know what type this is; I'm assuming string.
{
case "Jan":
model.Jan = assets.MonthlyChargeRate;
break;
case "Feb":
model.Feb = assets.MonthlyChargeRate;
// etc.
}
Third, if you're really feelin' fancy, you could use reflection to set the value.
第三,如果你真的感觉很奇特,你可以用反射来设定价值。
PropertyInfo prop = typeof(Asset).GetProperty(Month); // again, assuming string
prop.SetValue(model, assets.MonthlyChargeRage);
But I would encourage you to refactor the model. That will give you the best design, and remove the need for workarounds like options 2 and 3.
但我鼓励你重构模型。这将为您提供最佳设计,并消除对选项2和3等变通方法的需求。