简体   繁体   English

如何在C#中填充对象?

[英]How can I fill the object in C#?

I have object that have 26 data. 我有26个数据的对象。 In other side I have view model object. 另一方面,我有视图模型对象。 I want to pass these 26 data into view model. 我想将这26个数据传递到视图模型中。 Let me explain in code. 让我用代码解释。

            cheq = Service.Cheque.Instance.GetRejectedChequeInq(1, nationalcode, "", "", "").ReturnValue.ChequeItem.ToList();
            cust = Service.Cheque.Instance.GetRejectedChequeInq(1, nationalcode, "", "", "").ReturnValue;
            var cheqdto = new PageData<ChequeDTO>();
            var ss = new PageData<CustomerDTO>();
            cheqdto.Data = cheq;
            cheqdto.Total = cheq.Count;
            IdentifingInfo id = new IdentifingInfo();
            id.Name = cust.Name;
            id.RegisterPlace = cust.CDSbt;
            id.NationalCode = cust.IDNO;
            id.RegisterDate = cust.DTSbt;
            id.RegisterNumber = cust.NOSbt;
            id.RegisterPlace = cust.CDSbt;
            ChequeItemVM cheqItem = new ChequeItemVM();

            foreach (var i in cheq)
            {
                cheqItem.AccountNumber = i.ACCNTNO;
                cheqItem.Ammount = i.AMCHQ;
                cheqItem.BankId = i.CDBNK;
                cheqItem.Branch = i.CDSHB;
                cheqItem.BranchName = i.DESC;
                cheqItem.ChequeDate = i.DTCHQ;
                cheqItem.ChequeID = i.IDCHQ;
                cheqItem.ChequeNumber = i.NOCHQ;
                cheqItem.CurrencyAmount = i.CONVRATE;
                cheqItem.CurrencyCode = i.CDARZ;
                cheqItem.RejectDate = i.BCKDTCHQ;
            }

Here cheq object is my main object. 这里的cheq对象是我的主要对象。 I want to pass the cheq value in to cheqitem all 26 values. 我想将cheq值传递给cheqitem所有26个值。 I know it is a bad question but help me please. 我知道这是一个不好的问题,但请帮助我。

Maybe it is the right time to discover a bit of Linq: 也许是时候发现一些Linq了:

var cheques = cheq.Select(i => new ChequeItemVM()
    {
        AccountNumber = i.ACCNTNO,
        Ammount = i.AMCHQ,
        BankId = i.CDBNK,
        Branch = i.CDSHB,
        BranchName = i.DESC,
        ChequeDate = i.DTCHQ,
        ChequeID = i.IDCHQ,
        ChequeNumber = i.NOCHQ,
        CurrencyAmount = i.CONVRATE,
        CurrencyCode = i.CDARZ,
        RejectDate = i.BCKDTCHQ
    }).ToList();

EDIT: you may need to add using System.Linq; 编辑:您可能需要using System.Linq;添加using System.Linq; at the very top of your source file 在源文件的最顶部

You need to create List<T> and add items in it one by one in loop : 您需要创建List<T>并在循环中一个接一个地添加项目:

List<ChequeItemVM> cheques = new List<ChequeItemVM>(); // create a list


            foreach (var i in cheq)
            {
                ChequeItemVM cheqItem = new ChequeItemVM(); // create item

                cheqItem.AccountNumber = i.ACCNTNO;
                cheqItem.Ammount = i.AMCHQ;
                cheqItem.BankId = i.CDBNK;
                cheqItem.Branch = i.CDSHB;
                cheqItem.BranchName = i.DESC;
                cheqItem.ChequeDate = i.DTCHQ;
                cheqItem.ChequeID = i.IDCHQ;
                cheqItem.ChequeNumber = i.NOCHQ;
                cheqItem.CurrencyAmount = i.CONVRATE;
                cheqItem.CurrencyCode = i.CDARZ;
                cheqItem.RejectDate = i.BCKDTCHQ;

                cheques.Add(cheqItem); // adding one by one item in List
            }

I would suggest that, you should overload ChequeItemVM constructor to take input Cheque object and initialize values. 我建议,您应该重载ChequeItemVM构造函数以获取输入Check对象并初始化值。 So you can save the code duplication in case you need same code to be used in somewhere else in the project. 因此,如果您需要在项目的其他地方使用相同的代码,则可以节省代码重复。

Your ChequeItemVM class should look like this 您的ChequeItemVM类应如下所示

class ChequeItemVM
{

    //Properties

    public ChequeItemVM()
    {
    }

    public ChequeItemVM( Cheque chq)
    {
        AccountNumber = chq.ACCNTNO;
        Ammount = chq.AMCHQ;
        BankId = chq.CDBNK;
        Branch = chq.CDSHB;
        BranchName = chq.DESC;
        ChequeDate = chq.DTCHQ;
        ChequeID = chq.IDCHQ;
        ChequeNumber = chq.NOCHQ;
        CurrencyAmount = chq.CONVRATE;
        CurrencyCode = chq.CDARZ;
        RejectDate = chq.BCKDTCHQ;
    }
}

And you existing code should look like this 您现有的代码应如下所示

       var lstCheques = new List<ChequeItemVM>();


        foreach (var i in cheq)
        {
            lstCheques.Add(new ChequeItemVM(i));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM