0

I have a JS table that i populate using a Linq Statement. Im fairly new to LINQ and i really need this LINQ statement so that i can finish off my project. Here is my LINQ Statement. (That where statement is wrong because the database values are wrong, the linq will fix everything)

MSCDatabaseDataContext MSCDB = new MSCDatabaseDataContext();
var q = from row in MSCDB.Tbl_Campaigns
        join cs in MSCDB.tbl_StatusOfCampaigns on row.CampaignStatus equals cs.ID
        where ((row.CampaignStatus == 0) ||
           (row.CampaignStatus == 1) ||
           (row.CampaignStatus == 2) ||
           (row.CampaignStatus == 3))
        select new Campaign
        {
            CampaignID = row.CampaignId,
            CampaignName = row.CampaignName,
            Target = Convert.ToInt32(row.Target),
            Discount = Convert.ToInt32(row.Discount),
            CampaignSDate = Convert.ToDateTime(row.StartDate),
            CampaignEDate = Convert.ToDateTime(row.EndDate),
            CurrentStatus = replaceStatus((row.CampaignStatus.ToString())),
            Uptake = Convert.ToInt32(row.Uptake),
        };

I Basically want to remove that Where caluse and the inner join, And i can have a Case statement to display values based on dates.

CASE 
    WHEN EndDate >= GETDATE() and StartDate <= GETDATE() THEN 'Active' 
    WHEN StartDate >= GETDATE() THEN 'Pending'
    ELSE 'Closed' 
    END as 'CurrentStatus',

The help would be greatly appreciated. (You can remove the where caluse and inner join as it will not be needed.)

Domysee
  • 12,718
  • 10
  • 53
  • 84
Kei Francois
  • 145
  • 14

1 Answers1

2

by using the conditional operator you can solve this in one line

CurrentStatus = row.EndDate >= DateTime.Now && row.StartDate <= DateTime.Now ? "Active": row.StartDate >=  DateTime.Now ? "Pending": "Closed";
fubo
  • 44,811
  • 17
  • 103
  • 137
  • Could you lease help me adapt it to the code? (Im still a junior, really sorry about this.) @Fubo , Also Btw i dont have a column in the database as currentStatus, only CampaignStatus, which is explicitly converted to CurrentStatus. – Kei Francois Nov 12 '15 at 10:20
  • this line should be 'copy and paste' able just replace `CurrentStatus = replaceStatus((row.CampaignStatus.ToString())),` – fubo Nov 12 '15 at 10:21
  • Haha thank you so much @Fubo, you are a freaking genius ! :D – Kei Francois Nov 12 '15 at 10:34
  • Uhm ive heard that many times. STILL Dont know how to do that. where and how do i do this? __ EDIT : Never mind, found it. – Kei Francois Nov 12 '15 at 13:02