Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Adding items to list

This can't be hard but I can't figure it out. Tried several things. I know it's probably a type mismatch that I'm doing.

I have a table: FulfullmentBatch with UserId and ConfirmationCode

I have this loop. I want to populate batch.ConfirmationCode  = consumer.ConfirmationCode
and batch.userId = consumer.id in that loop. The "foreach" loop I have won't work, of course.

 public bool Update(IList<FulfillmentModel> model)
            {

                var batch = new List<FulfillmentBatch>();
                try
                {
                    //consumer
                    foreach (var f in model)
                    {
                      
                            var consumer =
                                _dataContext.Consumers.Where(m => ((m.Id == f.Id) && (f.Usertype == "C"))) //model has both consumer and dealer row. Use this flag to loop thru corresponding tables. Set in Stored proc
                                    .SingleOrDefault(m => m.ConfirmationCode == f.ConfirmationCode);

                            if (consumer != null) consumer.SubmissionStatusId = (int)StatusSubmission.Status.Fulfilled;
                          //*** populate here

                            //** this won't work because batch is a new list
                               foreach (var row in batch)
                        {
                            row.UserId = consumer.Id;
                            row.ConfirmationCode = consumer.ConfirmationCode;
                        }
                    }

                 

                _dataContext.SubmitChanges();

Open in new window

SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

btw, what is a list of batches: new List<FulfillmentBatch>()?

FulfillmentBatch is actually a table. I added it to datacontext. I want to insert into this table.

I don't think I should be using a table directly. I probably should have a class for it.

I have this model class which probably be used and then FulfillmentBatch be inserted into using this class?

 public class FulfillmentModel
    {

        public int Id { get; set; }
        public string ConfirmationCode {get; set;}
...
}

Open in new window


(I did something like your code but had a mistake in the new section)
I think this is what you want

batch.Add( new FulfillmentBatch() { UserId = consumer.Id, ConfirmationCode = consumer.ConfirmationCode } );

Open in new window


Best regards
This one developer here says I shouldn't use FulfillmentBatch directly because it's a table in the database. I should have a class (that FulfillmentModel class I posted above). Populate that model, then use the model to insert into
FulfillmentBatch table.

(I have a deadline tomorrow so the code above should get me going . I can refactor later)
@Camilla i reread your comments. You don't need a List  you need to add the records to table FullFilmentBatch. Is this correct?
that's correct, Walter.
So, that foreach I have, gets the rows, I update the rows in Consumer table. But then, I want the rows that were updated in Consumer table to be inserted in FulfillmentBatch table (only 2 columns tho..not all the columns).

I can do it with a stored proc but want to do it in LINQ.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me try, thanks. However, I would need the "batch" to be inserted AFTER  _dataContext.SubmitChanges();

This is just in case something fails in _dataContext.SubmitChanges(); line of code.

That's why I was using a list.
Actually, I can still use your code. I'll post back.
Yeah, changed it like this. Now I can insert after the update to Consumer table

  var batch = new FulfillmentBatch();
                try
                {
                    //consumer
                    foreach (var f in model)
                    {
                      
                            var consumer =
                                _dataContext.Consumers.Where(m => ((m.Id == f.Id) && (f.Usertype == "C"))) //model has both consumer and dealer row. Use this flag to loop thru corresponding tables. Set in Stored proc
                                    .SingleOrDefault(m => m.ConfirmationCode == f.ConfirmationCode);

                        if (consumer != null)
                        {
                            consumer.SubmissionStatusId = (int)StatusSubmission.Status.Fulfilled;
                            
                             //batch.Add(new FulfillmentBatch() { ConfirmationCode = consumer.ConfirmationCode, UserId = consumer.Id });

                             // ADDED
                              batch = new FulfillmentBatch() { UserId = consumer.Id, ConfirmationCode = consumer.ConfirmationCode };
                          
                        }

                    }

                _dataContext.SubmitChanges();
                _dataContext.FulfillmentBatches.InsertOnSubmit(batch);

Open in new window