内部连接使用LLBLGen?

时间:2010-07-01 16:07:46

标签: orm join llblgenpro

如何使用LLBLGen进行简单的连接?

table1 - clientTable(地址,电话等) table2 - employeeTable(名称等) table3 - clientEmployeeTable(clientid,employeeid)

我正在使用employeeId填写数据网格,其中包含客户信息(地址,电话等)的字段,我不知道如何使用LLBLGen检索此信息。我想我可以创建一个存储过程,但也许有一种更简单的方法吗?

我对LLBLGen完全陌生。

我一直在使用存储过程,但也许有更好的方法。

// in stored proc

SELECT (my specific fields)
FROM [client].[List] abl
    INNER JOIN [client].ClientGroup cg ON cg.ClientGroupId = abl.ClientGroupId


// in code 
DataTable dt=RetrievalProcedures.GetEmployeeNote(EmployeeId);
rgridNotes.DataSource = dt;

3 个答案:

答案 0 :(得分:1)

欢迎来到LLBLGen!一旦你上学,就会有很棒的产品。从各种连接表中获取信息的一种方法是使用您自己设计的类型列表。

您的“我的特定字段”在ResultsetFields中定义。您的WHERE子句是使用IPredicateExpression定义的。您的JOIN子句由IRelationCollection和每个实体的.Relations属性精心处理。

当试图加快速度时,运行SQL Server探查器(假设你正在使用MSSQL)是了解LLBLGen中代码更改如何影响服务器请求的实际SQL的关键。管理JOIN和WHERE子句中的所有()有时需要仔细排序,但大部分时间都是第一次尝试。这是我们使用的通用代码段:

private static DataTable GetTheGoodsOnEmployeeClients()
{
  // Define the fields that you want in your result DataTable
  ResultsetFields fields = new ResultsetFields(2);
  fields.DefineField(MyEntityFields.FieldName1, 0);
  fields.DefineField(MyEntityFields.FieldName2, 1, "Field Name Alias");

  // Add the WHERE clause to the query - "Condition" can be a literal or a variable passed into this method
  IPredicateExpression filter = new PredicateExpression();
  filter.Add(MyEntityFields.FieldName == "Condition");

  // Add all JOIN clauses to the relation collection
  IRelationCollection relations = new RelationCollection();
  relations.Add(MyEntity.Relations.FKRelationship);
  relations.Add(MyEntity.Relations.FKRelationship, JoinHint.Left);

  ISortExpression sort = new SortExpression();
  sort.Add(MyEntityFields.FieldName | SortOperator.Ascending);

  // Create the DataTable, DAO and fill the DataTable with the above query definition/parameters
  DataTable dt = new DataTable();
  TypedListDAO dao = new TypedListDAO();
  dao.GetMultiAsDataTable(fields, dt, 0, sort, filter, relations, false, null, null, 0, 0);

  return dt;
}        

您的具体需求:

SELECT (my specific fields)
FROM [client].[List] abl
    INNER JOIN [client].ClientGroup cg ON cg.ClientGroupId = abl.ClientGroupId

因此会成为:

private static DataTable GetTheGoodsOnEmployeeClients()
{
  // Define the fields that you want in your result DataTable
  ResultsetFields fields = new ResultsetFields(3);
  fields.DefineField(ClientFields.ClientId, 0);
  fields.DefineField(ClientFields.ClientName, 1, "Client");
  fields.DefineField(ClientGroupFields.ClientGroupName, 2, "Group");

  // Add all JOIN clauses to the relation collection
  IRelationCollection relations = new RelationCollection();
  relations.Add(ClientEntity.Relations.ClientGroupEntityUsingClientGroupId);

  // Create the DataTable, DAO and fill the DataTable with the above query definition/parameters
  DataTable dt = new DataTable();
  TypedListDAO dao = new TypedListDAO();
  dao.GetMultiAsDataTable(fields, dt, 0, null, null, relations, false, null, null, 0, 0);

  return dt;
}        

虽然这个似乎像许多代码一样做基本的事情,但是一旦你习惯了它,代码几乎就会写出来并且比编写管道要快得多。你永远不会回去。

答案 1 :(得分:0)

您可能想要在相关字段上创建一些“字段”。您可以将ClientGroup属性添加到客户端实体,以透明地访问它们。这仅适用于(m:1)关系中的直接相关字段。如果您想加入更深层次的加入,则必须使用类型化列表。

当您获取实体并且由于where语句而想要加入时,可以使用关系来连接表并构建谓词。

此致

答案 2 :(得分:0)

Use LinqMetaData

LinqMetaData metaData = new LinqMetaData();

if you use adapter mode

LinqMetaData metaData = new LinqMetaData(adapter);

var result = from a in metaData.TableA
               join b in metaData.TableB on a.Key equals b.Key
             where a.OtherField == value
             select a;
相关问题