我怎样才能绑定在加入到GridView的查询,有一个LINQ数据源与业务层和数据访问层 [英] How can I bind a query with a join to a GridView, with a Linq datasource with a business layer and data access layer

查看:192
本文介绍了我怎样才能绑定在加入到GridView的查询,有一个LINQ数据源与业务层和数据访问层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想一起加入两个表在GridView显示

I have two tables that I'd like to join together display in a GridView

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

bar
barID  barTitle
1      Developer
2      Project Manager

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

(relationships and constraints have been defined in the database)

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

I've got a GridView for foo that outputs like this

Name    Title
Alice   2
Bob     1
Charlie 1

但我想看到下面的

But I'd like see the following

Name    Title
Alice   Project Manager
Bob     Developer
Charlie Developer

显然,我的SQL看起来像:

Clearly, my SQL would look like:

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

深深地埋在我的code我有以下几点:

Buried deep in my code I have the following:

FooDAL.cs

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的加入,但我不知道该怎么做。我怎样才能modLINQ的code办理加入?

推荐答案

您可以使用此查询:

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

也网格您需要从改变

also in your grid you need to change from

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

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

等效LINQ查询是:

The equivalent LINQ query would be:

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

这篇关于我怎样才能绑定在加入到GridView的查询,有一个LINQ数据源与业务层和数据访问层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆