Kei Francois
Kei Francois

Reputation: 145

How to add a Case statement to my LINQ Query

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.)

Upvotes: 0

Views: 837

Answers (1)

fubo
fubo

Reputation: 45947

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";

Upvotes: 2

Related Questions