0

我有两个要连接在一起的表格显示在 GridView 中

foo
fooID  fooName barID
1      Alice   2
2      Bob     1
3      Charlie 1

bar
barID  barTitle
1      Developer
2      Project Manager

(关系和约束已在数据库中定义)

我有一个foo像这样的输出的 GridView

Name    Title
Alice   2
Bob     1
Charlie 1

但我想看看以下

Name    Title
Alice   Project Manager
Bob     Developer
Charlie Developer

显然,我的 SQL 看起来像:

select fooName as Name, barTitle as Title
from foo join bar on foo.barid = bar.barid

深埋在我的代码中,我有以下内容:

FooDAL.cs

// ...
namespace ourDAL{
// ...
public class fooDAL : ICRUD <foo>, IDisposable //ICRUD is just an interface
{
// ...
  ourPoco context; // ourPoco was auto generated by a template from our  
                   // database and an EDMX file.

  public fooDAL()
  {
      context = new ourPoco;
  }
// ...
    public IList<Cfoo> FindAll()
    {
        return (from c in context.foo
                select c).ToList<foo>();
    }
// ...

我认为在这里我应该能够在 Linq 中构建一个 Join,但我不知道该怎么做。如何修改 Linq 代码来处理加入?

4

1 回答 1

1

您可以使用此查询:

select  a.fooName as Name, b.barTitle as Title from foo a inner join bar b
on a.barid= b.barid

同样在您的网格中,您需要从

<asp:BoundField DataField="BarID" HeaderText="Title" />

<asp:BoundField DataField="Title" HeaderText="Title" />

等效的 LINQ 查询将是:

var a = (from p in foo 
            join q in bar 
            on p.barid equals q.barid 
            select new { Name= p.fooName , Title = q.barTitle }).ToList();
于 2013-03-01T04:25:49.137 回答