简体   繁体   中英

How can I fill the object in C#?

I have object that have 26 data. In other side I have view model object. I want to pass these 26 data into view model. 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. I want to pass the cheq value in to cheqitem all 26 values. I know it is a bad question but help me please.

Maybe it is the right time to discover a bit of 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; 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<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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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