How to add an object to the database from the IEnumerable collection?

I have a collection of ienumerable objects to be added to the database, but it seems some conversion is required. Can someone point me in the right direction?

bool InsertDetails(DataTable detailTable, string fileName)
{
    using (SunseapEBTContext context = new SunseapEBTContext())
    {
        if (InsertMaster(fileName))//if creating master record successful
        {                    
            int masterId = GetSPReadingM(m => m.FileName == fileName).SPReadingMasterId; //get MasterID of file uploaded

             var details = detailTable.AsEnumerable().Select(row => new LeasingSPReadingDetailEntity()//new entity from datatable
             {
                //SPReadingId = row.Field<long>("ProductID"),

                        SPReadingMasterId = masterId,
                        BillCycleYear = int.Parse(row.Field<int>("Bill Cycle").ToString().Substring(0, 4)),
                        BillCycleMonth = byte.Parse(row.Field<byte>("Bill Cycle").ToString().Substring(4))

            });
            foreach(IEnumerable<LeasingSPReadingDetailEntity> detail  in details)
            {                       
                context.LeasingSPReadingDetailEntities.AddObject(detail);
            }
            context.SaveChanges();                    
        }
        return true;
    }
}

In foreach loop, an exception is thrown

CS1503 Argument 1: Cannot be converted from 'System.Collections.Generic.IEnumerable' to 'SunseapEBT.Domain.BillingModule.LeasingContract.Entity.LeasingSPReadingDetailEntity'

LeasingSPReadingDetailEntity class:

public class LeasingSPReadingDetailEntity
{
    public long SPReadingId { get; set; }
    public int SPReadingMasterId { get; set; }
    public int BillCycleYear { get; set; }
    public byte BillCycleMonth { get; set; }

}

: , , . masterId . .

: , . . , , . !

+4
3

@slawekwin - . , , , 2x: 1st, Enumerable ( ), 2nd .

.

foreach(var row in detailTable.AsEnumerable())
{                       
    context.LeasingSPReadingDetailEntities.AddObject(
        new LeasingSPReadingDetailEntity()//new entity from datatable
        {
           //SPReadingId = row.Field<long>("ProductID"),

           SPReadingMasterId = masterId,
           BillCycleYear = int.Parse(row.Field<string>("Bill Cycle").Substring(0, 4)),
           BillCycleMonth = byte.Parse(row.Field<string>("Bill Cycle").Substring(4)),
           AccountNumber = row.Field<string>("Account No."),
           PeriodStart = row.Field<DateTime>("Period Start"),
           PeriodEnd = row.Field<DateTime>("Period End"),
           TownCouncil = row.Field<string>("Customer Name"),
           Service = row.Field<string>("Service Type"),
           Adjustment = row.Field<string>("Adjustment"),
           Block = row.Field<string>("Blk"),
           AddressLine1 = row.Field<string>("Adress Line 1"),
           AddressLine2 = row.Field<string>("Adress Line 2"),
           AddressLine3 = row.Field<string>("Postal Code"),
           Usage = row.Field<decimal>("Usage"),
           Rate = row.Field<decimal>("Rate"),
           Amount = row.Field<decimal>("Amount")
        }
    );
}

--- EDIT ---

, , " " int, . , , . , :

BillCycleYear = int.Parse(row.Field<string>("Bill Cycle").Substring(0, 4)),
BillCycleMonth = byte.Parse(row.Field<string>("Bill Cycle").Substring(4)),
+2

, .

var details = detailTable.AsEnumerable().Select(row => new  LeasingSPReadingDetailEntity()//new entity from datatable
         {
            SPReadingId = row.Field<long>("ProductID"),
            SPReadingMasterId = masterId,
            BillCycleYear = int.Parse(row.Field<int>("Bill Cycle").ToString().Substring(0, 4)),
            BillCycleMonth = byte.Parse(row.Field<byte>("Bill Cycle").ToString().Substring(4)),
        }).ToList();
        foreach(var detail  in details)
        {                       
            context.LeasingSPReadingDetailEntities.Add(detail);
        }

:

var details = detailTable.AsEnumerable().Select(row => new  LeasingSPReadingDetailEntity()//new entity from datatable
         {
            SPReadingId = row.Field<long>("ProductID"),
            SPReadingMasterId = masterId,
            BillCycleYear = int.Parse(row.Field<int>("Bill Cycle").ToString().Substring(0, 4)),
            BillCycleMonth = byte.Parse(row.Field<byte>("Bill Cycle").ToString().Substring(4)),
        }).ToList();

context.LeasingSPReadingDetailEntities.AddRange(details);
+2

foreach . , .

            foreach(var detail  in details)
            {                       
                context.LeasingSPReadingDetailEntities.AddObject(detail);
            }

            foreach(LeasingSPReadingDetailEntity detail  in details)
            {                       
                context.LeasingSPReadingDetailEntities.AddObject(detail);
            }

If you look at the loop syntax foreach, the very first construct variableTypeis the type of the element that is stored in the collection ( LeasingSPReadingDetailEntityin your case) and NOT the type of the collection. You did this later, so you get an invalid error at startup.

foreach(variableType currentElementBeingIterated in collection){

    //code block to operate on currentElement

  }
+2
source

Source: https://habr.com/ru/post/1651485/


All Articles