简体   繁体   English

使用LINQ通过联接从两个表中获取信息

[英]Using LINQ to get information from two tables, with a join

I have a linq statement to populate two labels. 我有一个linq语句来填充两个标签。 The thing is, this information comes from two tables. 问题是,此信息来自两个表。 I Have a join to join the two tables, except i cant get my Terms and Conditions from my Campaign table. 我有一个联接来联接这两个表,除了我无法从Campaign表中获得我的条款和条件。 Its only picking up the RedemptionLog table columns. 它仅选择RedemptionLog表列。 Anyone to help with this? 有人帮忙吗?

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

For the SeshVoucherDisplay variable, it only picks up from the RedemptionLogs table, yet i did a join? 对于SeshVoucherDisplay变量,它仅从RedemptionLogs表中获取,但是我进行了联接吗? Any help? 有什么帮助吗?

Try something like this : 尝试这样的事情:

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

The column names are just for example purpose. 列名仅用于示例目的。 Put your own there. 把你自己的放在那里。 And thereafter, you can apply First on it and use that particular variable. 然后,您可以在其上应用First并使用该特定变量。

Hope this helps. 希望这可以帮助。

Well, by writing select row you asked LINQ to give back to you only row . 好吧,通过写select row您要求LINQ仅将row还给您。

If you want both elements, you need to ask for both of them, eg by writing select new { row, d } . 如果您需要这两个元素,则需要同时使用这两个元素,例如,编写select new { row, d }

In this example 在这个例子中

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

In baz you'll find only a list of foo s, in qux you'll find a list of both foo s and their bar . baz您只会找到foo的列表,而在qux您会找到foo s及其bar的列表。

Try this: 尝试这个:

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

When writing select row you relate to the row you defined in from row in MSCDB.Tbl_RedemptionLogs . 写入select row您与from row in MSCDB.Tbl_RedemptionLogsfrom row in MSCDB.Tbl_RedemptionLogs定义的from row in MSCDB.Tbl_RedemptionLogs

However if you want the data from both tables you have to write something similar to this: 但是,如果要从两个表中获取数据,则必须编写类似于以下内容的内容:

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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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