如何将LINQ中的多行与两个表连接起来?

时间:2016-01-27 17:18:32

标签: c# asp.net linq

我尝试将一行中的多行连接到2个表中,其中一个表有一个外键

我有这张表:

台式打印机:

PrinterID    Name      Description 
--------------------------------
1          Printer1   Description1
2          Printer2   Description2

表格分辨率:

ResolutionID    Measure    PrinterId
--------------------------------
1              123            1
2              234            1
3              345            2
4              456            2

我在GridView中需要这个:

PrinterID          Name              Description          Resolution
------------------------------------------------------------------------
1                 Printer1           Description1          123, 234
2                 Printer2           Description2          345, 456

我有这段代码,但我被困了

    var Printer = from tPrinter in Context.Printer
                  join tResolution in Context.Resolution on tPrinter.PrinterId equals tResolution.PrinterId into collection2
                  from subcase2 in collection2.DefaultIfEmpty()
                  where tPrinter.DisableDate == null

                  select new
                  {
                      tPrinterPrinterId = tPrinter.PrinterId,
                      tPrinterName = tPrinter.Name,
                      tPrinterDescription = tPrinter.Description,
                      tCountryName = tPrinter.City1.State.Country.Name,
                      tStateName = tPrinter.City1.State.Name,
                      tCityName = tPrinter.City1.Name
                  };

  return Printer

3 个答案:

答案 0 :(得分:0)

连接为每个打印机和分辨率组合创建一行。我想你想在子查询中选择它。

var Printer = from tPrinter in Context.Printer
              where tPrinter.DisableDate == null

              select new
              {
                  tPrinterPrinterId = tPrinter.PrinterId,
                  tPrinterName = tPrinter.Name,
                  tPrinterDescription = tPrinter.Description,
                  tCountryName = tPrinter.City1.State.Country.Name,
                  tStateName = tPrinter.City1.State.Name,
                  tCityName = tPrinter.City1.Name,
                  resolution = Context.Resolution.Where(r => r.PrinterId == tPrinter.PrinterId)
              };

return Printer;

完成ToList()以将其从数据库中删除后,您可以String.Join(", ", printer.resolution)创建逗号分隔列表。

答案 1 :(得分:0)

var Printer = Context.Printer.Join(Context.Resolution, p => p.PrinterID, r => r.PrinterId, (p, r) => new
        {
            PrinterId = p.PrinterID,
            Name=p.Name,
            Description=p.Description,
            Resolution = String.Join(", ", Context.Resolution.Where(k => k.PrinterId == p.PrinterID).Select(lm => lm.Measure.ToString()))
        }).Distinct();

答案 2 :(得分:0)

这可能不是您要求的,但如果您使用模型绑定,则可以在没有任何复杂linq的情况下显示数据。这是有效的,因为模型应该知道打印机有一个与之关联的Resolutions集合(来自外键关系)。

<asp:GridView ID="GridView1" runat="server" ItemType="Business.Models.Printer" SelectMethod="SelectPrinters" Style="margin-left: 30px" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="PrinterID" HeaderText="Id"/>
        <asp:TemplateField HeaderText="Resolution">
            <ItemTemplate>
                <asp:DataList ID="Resolutions"
                    RepeatDirection="Horizontal"
                    RepeatLayout="Table"
                    RepeatColumns="0" runat="server" ItemType="Business.Models.Resolution" DataKeyNames="ResolutionID" DataSource='<%# Item.Resolutions %>' >
                    <ItemTemplate>
                        <asp:Label ID="lblResolutions" runat="server"> <%#Item.Resolution.Measure %></asp:Label>
                    </ItemTemplate>

                    <SeparatorTemplate>
                        ,
                    </SeparatorTemplate>

                </asp:DataList>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

选择方法:

    public IQueryable<Printers> SelectPrinters()
    {
        var query = dbContext.Printers;
        return query;
    }