如何在LINQ中进行内部加入

本文关键字:内部 LINQ | 更新日期: 2023-09-27 18:22:51

我有一个Linq语句来将数据检索到我的JAVASCRIPT表中。我想做的是显示市场活动的存档人。我需要将其加入到我的MSCDB.Tbl_Users中的市场活动表中。有人能帮我一下吗?我是林克的新手。

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)
                    };

如何在LINQ中进行内部加入

我不确定数据库结构是什么样子的,但如果Tbl_UsersCampaignId,你应该这样做:

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)
    };

示例代码为

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