WCF RIA - 加入两个表

时间:2012-07-27 00:36:41

标签: silverlight ria

我找到了很多关于这个问题的解释,但没有什么能真正帮助我。事情很简单。我的dataModel上有两个表:Events和TimeStamps,都有字段EntryID,这是它们之间的关系(表实际上是Views,我不能对DB进行更改,我只能查询它们。)我的domainService,我有创建的方法来从每个表中获取数据。到目前为止,我能够使用来自其中一个表的数据填充dataGrid,但我真正需要的是从两个表中显示。在T-SQL中,它将类似于:

  Select e.EntryID,t.closed_time
  from Events e inner join TimeStamps t
  on e.EntryID=t.EntryID

所以我想在我的dataGrid上显示Entry_ID和closed_time.I感谢您帮助解决我的问题

我尝试了一个新的自定义类

    public class CustomTable
    {
        public string EntryId { get; set; }
        public int closed_time { get; set; }
    }

    public IQueryable<CustomTable> GetJoined()
    {
        return (from i in this.ObjectContext.Events                   
                join p in this.ObjectContext.TimeStamps p
                on i.Entry_ID equals p.Entry_ID
                select new CustomTable
                {
                    EntryId = i.Entry_ID,
                    closed_Time = p.Closed_TIME
                });
    }

这是我自己添加的附加代码,我很确定缺少某些内容,此方法和类本身已添加到我的service.cs上

1 个答案:

答案 0 :(得分:0)

这是完成的最终代码和程序,不要忘记在每个步骤之后构建项目:

1-在Myproject.Web下打开一个新课程(添加 - >新项目 - &gt;类)

namespace Myproject.Web
{

    public class CustomTable
    {
        [Key]
        public string EntryId { get; set; }
        public int closed_Time { get; set; }
    }
}

2 - 在IncidentService.cs上添加:

public IQueryable<CustomTable> GetJoined()
{
    return (from i in this.ObjectContext.Events                   
            join p in this.ObjectContext.TimeStamps p
            on i.Entry_ID equals p.Entry_ID
            select new CustomTable
            {
                EntryId = i.Entry_ID,
                closed_Time = p.Closed_TIME
            });
}

3 - 在Mypage.xaml.cs上添加

public MyPage()
{
    InitializeComponent();


    this.dataGrid1.ItemsSource = _IncidentContext.CustomTables;
    _IncidentContext.Load(_IncidentContext.GetJoinedQuery());



    DataGridTextColumn entry = new DataGridTextColumn();
    entry.Binding = new System.Windows.Data.Binding("EntryId");
    entry.Header = "Entry Id";

    DataGridTextColumn closed = new DataGridTextColumn();
    closed.Binding = new System.Windows.Data.Binding("closed_Time");
    closed.Header = "Closed Time";

    dataGrid1.Columns.Add(entry);
    dataGrid1.Columns.Add(closed);

}

我希望这会帮助其他人解决同样的问题,我花了3天时间研究这个解决方案!!