使用LINQ从两个表中获取信息,并使用连接

时间:2015-11-27 10:57:51

标签: c# linq join

我有一个linq语句来填充两个标签。问题是,这些信息来自两个表格。我有一个加入加入这两个表,除了我不能从我的Campaign表中获取我的条款和条件。它只获取了RedemptionLog表列。有人帮忙吗?

MSCDatabaseDataContext MSCDB = new MSCDatabaseDataContext();
            var q = from row in MSCDB.Tbl_RedemptionLogs 
                    join d in MSCDB.Tbl_Campaigns on row.CampaignId equals d.CampaignId
                    orderby row.VoucherCode descending
                    select row;

            var SeshVoucherDisplay = q.First();

            lblCode.Text = SeshVoucherDisplay.VoucherCode;
            lblTerms.Text = SeshVoucherDisplay

对于SeshVoucherDisplay变量,它只从RedemptionLogs表中获取,但我做了一个连接?有帮助吗?

4 个答案:

答案 0 :(得分:1)

尝试这样的事情:

var SupJoin = from row in MSCDB.Tbl_RedemptionLogs
              join d in MSCDB.Tbl_Campaigns on row.CampaignId equals d.CampaignId
              orderby row.VoucherCode descending
              select new { Id = row.ID, SupplierName = row.SupplierName, 
               CustomerName = d.CompanyName };

列名仅用于示例目的。把你自己放在那里。此后,您可以对其应用First并使用该特定变量。

希望这会有所帮助。

答案 1 :(得分:1)

好吧,通过撰写select row,您要求LINQ仅回复row

如果您想要这两个元素,则需要同时询问这两个元素,例如:写select new { row, d }

在这个例子中

var foo =
    new []
    {
        new { Id = 1, Name = "a" },
        new { Id = 2, Name = "b" },
        new { Id = 3, Name = "c" }
    };

var bar =
    new []
    {
        new { Id = 1, Name = "d" },
        new { Id = 2, Name = "e" },
        new { Id = 3, Name = "f" }
    };

var baz =
    from a in foo
    join b in bar on a.Id equals b.Id
    select new { a, b };

var qux =
    from a in foo
    join b in bar on a.Id equals b.Id
    select new { a, b };

baz中,您只会找到foo的列表,在qux中,您会找到foo及其bar的列表<form class="send-with-ajax" method="post" action="send_form_email.php"> <div class="row"> <div class="three columns alpha"> <label for="first_name">First Name <span>required</span></label> <label for="last_name">Last Name <span>required</span></label> </div> <div class="seven columns omega"> <input type="text" required placeholder="First Name" id="first_name" name="name"> <input type="text" required placeholder="Last Name" id="last_name" name="name"> </div> </div> <div class="row"> <div class="three columns alpha"> <label for="email">Your Email <span>email required</span></label> </div> <div class="seven columns omega"> <input type="email" required placeholder="your@email" id="email" name="email"> </div> </div> <div class="row"> <div class="three columns alpha"> <label for="comments">Message</label> </div> <div class="seven columns omega"> <textarea placeholder="Enter you message, and please include your artist name. you can put either or choose between Pervis or Duijuan" id="comments" name="comments"></textarea> </div> </div> <div class="seven columns offset-by-three"> <button type="submit">Submit Form</button> <div class="ajax-response"></div> </div> </form>

答案 2 :(得分:1)

试试这个:

var query = (from row in MSCDB.Tbl_RedemptionLogs
                                 join d in MSCDB.Tbl_Campaigns on row.CampaignId equals d.CampaignId)
                                 orderby row.VoucherCode descending
                                 select new
                                 {
                                    columnname = row.columnname
                                 });

答案 3 :(得分:0)

撰写select row时,您与from row in MSCDB.Tbl_RedemptionLogs中定义的行相关。

但是,如果你想要两个表中的数据,你必须写一些类似的东西:

select new { 
    // the properties of row
    Redemption = row.redemption, 
    // the properties of d
    Campaign = d.CampaignID // alternativly use may also use row.CampaignID, but I wanted to show you may acces all the members from d also 
}