SolvedPrivate

Adding items to list

Posted on 2014-12-04
11
42 Views
Last Modified: 2016-02-18
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

0
Comment
Question by:Camillia
  • 7
  • 3
11 Comments
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 40481043
Why a loop? "Populate" normally means adding. Thus instead of this loop, a

batch.Add(new FulfillmentBatch() { ConfirmationCode  = consumer.ConfirmationCode, userId = consumer.id }); 

Open in new window


should work.

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

Author Comment

by:Camillia
ID: 40481060
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)
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40481074
I think this is what you want

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

Open in new window


Best regards
0
 
LVL 7

Author Comment

by:Camillia
ID: 40481094
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)
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40481100
@Camilla i reread your comments. You don't need a List  you need to add the records to table FullFilmentBatch. Is this correct?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:Camillia
ID: 40481104
that's correct, Walter.
0
 
LVL 7

Author Comment

by:Camillia
ID: 40481116
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.
0
 
LVL 10

Accepted Solution

by:
Walter Padrón earned 250 total points
ID: 40481128
This should work

 public bool Update(IList<FulfillmentModel> model)
{

//DELETED:                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

            // ADDED
            var batch = new FulfillmentBatch() { UserId = consumer.Id, ConfirmationCode = consumer.ConfirmationCode };
            _datacontext.FullFilmentBatchTABLENAME.Add( batch );            
        }
    
        _dataContext.SubmitChanges();
    }
}                                  

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 40481158
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.
0
 
LVL 7

Author Comment

by:Camillia
ID: 40481310
Actually, I can still use your code. I'll post back.
0
 
LVL 7

Author Comment

by:Camillia
ID: 40481332
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

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Class Library Dynamics For Connectstring Information 2 21
What is Thread.Sleep? 5 46
WKHTMLTOPDF - --disable-smart-shrinking not working 10 36
Tool Box 2 34
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now