0

この質問の言い方がよくわからないので、ご容赦ください。必要に応じて編集します。

私のコードから始めましょう:

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();

                }
         }

ユーザーから請求したい月を取得し、その月の列に月額料金を挿入したいと考えています。私の人生でこれを行う方法がわかりません。何か提案があるか、どこから探し始めることができるか教えてください。

ありがとう!

EDIT*** テーブルのモデル

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; }
4

1 に答える 1

2

さて、あなたにはいくつかの選択肢があります。

1つ目は、モデルと場合によってはデータベースをリファクタリングすることです。これにより、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;}
 }

2つ目は、古き良きswitchステートメントを利用することです。

 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.
}

第三に、あなたが本当に空想を感じているなら、あなたは値を設定するために反射を使うことができます。

 PropertyInfo prop = typeof(Asset).GetProperty(Month); // again, assuming string
 prop.SetValue(model, assets.MonthlyChargeRage);

ただし、モデルをリファクタリングすることをお勧めします。これにより、最適な設計が得られ、オプション2や3などの回避策が不要になります。

于 2013-02-15T18:35:25.110 に答える