【问题标题】:Inner joins using LLBLGen?使用 LLBLGen 进行内部连接?
【发布时间】:2010-07-01 16:07:46
【问题描述】:

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

table1 - clientTable(地址、电话等) table2 - 员工表(姓名等) 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;

【问题讨论】:

    标签: orm join llblgenpro


    【解决方案1】:

    欢迎来到 LLBLGen!受过教育的好产品。从各种连接表中获取信息的一种方法是使用您自己设计的类型化列表。

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

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

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

    虽然这似乎需要很多代码来做一些基本的事情,但一旦你习惯了它,这些代码几乎可以自己编写,而且比编写管道要快得多。你永远不会回去。

    【讨论】:

      【解决方案2】:

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

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

      问候

      【讨论】:

      • 我想我只是想弄清楚如何使用关系类型。我一直很难在 llblgen 上找到好的文档。
      • 什么,除了 LLBLGen 自己的文档和用户支持论坛 - 这里经常引用作为使用 LLBLGen 的主要原因之一 :-)
      【解决方案3】:
      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;
      

      【讨论】:

        猜你喜欢
        • 2013-11-19
        • 2014-05-31
        • 2012-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        相关资源
        最近更新 更多