-1

I have two collections

var campaigns = new List<Campaigns>();
IEnumerable<CampaignsDb> campaignsFromDB = db.Campaigns
        .Where(c => (c.IsDeleted == false))
        .OrderBy(c => c.ScheduleTime)
        .ToArray();

Next I'm filling one collection from another using foreach() :

foreach (var campaign in campaignsFromDB)
{
    campaigns.Add(new Campaigns { CampaignID = campaign.CampaignID, OwnerID = campaign.CreatedBy, AccountID = campaign.AccountID });
}

Can I use Select() linq method instead of foreach loop ?

mic kos
  • 29
  • 1
  • 4

3 Answers3

0

Unless you need the original empty list to start with (e.g. to add some other campaigns first), you can use:

var campaigns = campaignsFromDB
    .Select(c => new Campaigns
           {
               CampaignID = c.CampaignID,
               OwnerID = c.CreatedBy,
               AccountID = c.AccountID
           })
    .ToList();

However, that will still have fetched the complete campaign information from the database in order to populate campaignsFromDB. That's fine if you need that array for some other reason, but if not, you could make it more efficient by putting the projection into the query:

var campaigns = db.Campaigns
    .Where(c => !c.IsDeleted)
    .OrderBy(c => c.ScheduleTime)
    .Select(c => new Campaigns
           {
               CampaignID = c.CampaignID,
               OwnerID = c.CreatedBy,
               AccountID = c.AccountID
           })
    .ToList();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Dr.Snail: The first snippet uses `campaignsFromDB`, which is already filtered. The second snippet uses `db.Campaigns`, including the filtering and ordering. – Jon Skeet Aug 06 '18 at 09:39
0

Yes, like this:

var campaigns = db.Campaigns
    .Where(c => !c.IsDeleted)
    .OrderBy(c => c.ScheduleTime)
    .Select(c => new Campaigns
    {
        CampaignID = c.CampaignID,
        OwnerID = c.CreatedBy, 
        AccountID = c.AccountID
    })
    .ToList();
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Note that if the OP needs `campaignsFromDB` for another reason, this will make an unnecessary query to the database. Probably worth mentioning as a single line, at least – Jon Skeet Aug 06 '18 at 09:42
  • Well we don't even know this is using EF so I'm not going to add that. It probably is of course, but with the info we have... – DavidG Aug 06 '18 at 09:43
0

Yes. Just

var campaigns = db.Campaigns
        .Where(c => (c.IsDeleted == false))
        .OrderBy(c => c.ScheduleTime)
        .Select(c => new Campaigns { CampaignID = c.CampaignID, OwnerID = c.CreatedBy, AccountID = c.AccountID })
        .ToList();

or if you need the source set from the database too

var campaignsFromDB = db.Campaigns
        .Where(c => (c.IsDeleted == false))
        .OrderBy(c => c.ScheduleTime)
        .ToArray();

var campaigns = campaignsFromDB
        .Select(c => new Campaigns { CampaignID = c.CampaignID, OwnerID = c.CreatedBy, AccountID = c.AccountID })
        .ToList();
  • Your contribution is appreciated, but what does it add that the existing answers don't already cover? – DavidG Aug 06 '18 at 10:08