【问题标题】:Silverlight using Entity to LINQ on JoinsSilverlight 在连接上使用 Entity to LINQ
【发布时间】:2011-06-19 19:22:47
【问题描述】:

我有以下代码,它可以为我获取单个实体的数据。

LoadOperation<TimeForm> loadoperation = _cc.Load(_cc.GetTimeFormsQuery()
           .Where(o => o.Start>= weekrange.startdate 
                    && o.End<= weekrange.enddate 
                    && o.USERID== "TEST"));

但是我有 3 个表连接到这个 TimeForm 表,在 sql 中我的查询如下所示:

SELECT T* FROM TimeForm 
INNER JOIN CLIENT ON TimeForm.CODEID= CLIENT.CODEID 
INNER JOIN RATE ON TimeForm.RATEID= RATE.RATEID 
INNER JOIN TASK ON TimeForm.TASKID = TASK.TASKID  

使用上面的语法怎么可能呢?我需要这些表中的一些值。

【问题讨论】:

    标签: c# silverlight wcf entity ria


    【解决方案1】:

    试试这样的:

    var query = context.TimeForm.
                Join(context.CLIENT,
                t => t.CODEID, c => c.CODEID ,
                (t, c) => new
                {
                    PropertyA = t.ColumnA,
                    PropertyB = c.ColumnB                    
                }).Join(context.RATE,
                        b => b.RATEID, r => r.RATEID,
                        (b, r) => new
                        {
                            PropertyC = b.ColumnC,
                            PropertyD = r.ColumnD                            
                        }).Join(context.TASK,
                               x => x.TASKID, t => t.TASKID,
                               (x,t) => new
                               {
                                   PropertyE = x.ColumnE,
                                   PropertyF = t.ColumnF
                               });
    

    PropertyA、B 等只是类型中存在的属性,用于存储从查询返回的数据。而 ColumnA、B 等是连接中涉及的表中存在的列。您可以在查询中用实际值替换这些值。

    【讨论】:

      【解决方案2】:

      您需要转到域服务文件(其中定义了 GetTimeFormsQuery())。它看起来像:

      public IQueryable<TimeForm> GetTimeForms() {
          return this.Context.TimeForm;
      }
      

      ,并添加到它,所以它是这样的:

      public IQueryable<TimeForm> GetTimeForms() {
          return this.Context.TimeForm
              .Include("Client") // Assuming your property to see the client is called "Client"
              .Include("Rate") // Same for "Rate"
              .Include("Task"); // and "Task
      }
      

      或者在你的 TimeFrom 实体中调用任何导航属性。

      Silverlight 不执行延迟加载,因此您必须在域服务的查询中显式包含这些属性。此外,在接受开始和结束日期以及用户 ID 的域服务上创建一个额外的方法可能是明智的,这样您就不会每次都将整个表拉过网络。

      public IQueryable<TimeForm> GetTimeFormsWithStartAndEnd(DateTime start, DateTime end, string userId) {
          return this.Context.TimeForm
              .Include("Client") // Assuming your property to see the client is called "Client"
              .Include("Rate") // Same for "Rate"
              .Include("Task") // and "Task
              .Where(o => o.Start>= start 
                      && o.End<= end 
                      && o.USERID== userId));
      
      }
      

      重建您的网络项目后,您的 silverlight 中将有一个名为 GetTimeFormsWithStartAndEndQuery 的方法,其中这 3 个作为参数。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多