简体   繁体   中英

How to do an Inner Join in LINQ

I have a Linq statement to retrieve data into my JAVASCRIPT table. What i want to do is display who the campaign was archived by. I need to join it to my MSCDB.Tbl_Users onto the Campaign table. Could anyone try give me some help with this? I am fairly new to LINQ.

MSCDatabaseDataContext MSCDB = new MSCDatabaseDataContext();
            var q = from row in MSCDB.Tbl_Campaigns
                    where row.CampaignStatus == 4
                    select new ArchiveReport

                    {
                        CampaignId = row.CampaignId,
                        CampaignName = row.CampaignName,
                        CampaignDescription = row.CampaignDescription,
                        CurrentStatus = row.EndDate >= DateTime.Now && row.StartDate <= DateTime.Now ? "Active" : row.StartDate >= DateTime.Now ? "Pending" : row.CampaignStatus == 4 ? "Archived" : "Closed",
                        CampaignStartDatesS = Convert.ToDateTime(row.StartDate).Date + " - " + Convert.ToDateTime(row.EndDate).Date,
                        Discount = Convert.ToInt32(row.Discount),
                        Target = Convert.ToInt32(row.Target),
                        Uptake = Convert.ToInt32(row.Uptake),
                        DateArchived = Convert.ToDateTime(row.DateArchived),
                        ArchivedBy = Convert.ToInt32(row.ArchivedBy)
                    };

I'm not sure how you db structure looks like but if Tbl_Users have CampaignId you should do it like this:

MSCDatabaseDataContext MSCDB = new MSCDatabaseDataContext();
var q = from row in MSCDB.Tbl_Campaigns
    //here is your join
    join usr in MSCDB.Tbl_Users on row.CampaignId equals usr.CampaignId
    //-------------------------
    where row.CampaignStatus == 4
    select new ArchiveReport

    {
        //Here how you can jet values from user table
        UserName = usr.Name,
        //-------------------------
        CampaignId = row.CampaignId,
        CampaignName = row.CampaignName,
        CampaignDescription = row.CampaignDescription,
        CurrentStatus = row.EndDate >= DateTime.Now && row.StartDate <= DateTime.Now ? "Active" : row.StartDate >= DateTime.Now ? "Pending" : row.CampaignStatus == 4 ? "Archived" : "Closed",
        CampaignStartDatesS = Convert.ToDateTime(row.StartDate).Date + " - " + Convert.ToDateTime(row.EndDate).Date,
        Discount = Convert.ToInt32(row.Discount),
        Target = Convert.ToInt32(row.Target),
        Uptake = Convert.ToInt32(row.Uptake),
        DateArchived = Convert.ToDateTime(row.DateArchived),
        ArchivedBy = Convert.ToInt32(row.ArchivedBy)
    };

Sample code is

var q= from row in MSCDB.Tbl_Campaigns
                join user in MSCDB.Tbl_Users 
                on row.UserId equals user.ID
                     select row;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM