将多个IQueryable合并在一起 - Silverlight

时间:2013-02-01 15:15:19

标签: silverlight ria iqueryable domainservices

窥视,

我是Silverlight的重生处女,所以请耐心等待我。我有两个单独的DomainServices,指向两个不同的SQL数据库服务器。在这些域服务中,我在每个域服务中设置了一些IQueryable。

我需要从单独的DomainServices合并两个IQueryable。我相信这必须是可行的。

以下是两个域名服务。

我想将GetCustomCallLogs(来自HEATLiveDomainService)与GetTblCallsLogged(来自HeatDomainService)合并。 GetCustomCallLogs中的键是CallID,GetTblCallsLogged中的键是RecID。

如果我可以理解,我需要创建一个新类型来考虑两个连接表中的任何字段。

希望香港专业教育学院解释我的情景,我不会愚蠢。

提前致谢

  public class HEATLIVEDomainService : LinqToEntitiesDomainService<HeatIT9_LiveEntities>
{

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Assignees' query.
    public IQueryable<Assignee> GetAssignees()
    {
        return this.ObjectContext.Assignees;
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'CallLogs' query.
    public IQueryable<CallLog> GetCallLogs()
    {
        //            return this.ObjectContext.CallLogs.Where(c => DateTime.Parse(c.ClosedDate).Year == 2012 && c.CallStatus == "Closed" && c.ClosedBy.Length > 0);
        return this.ObjectContext.CallLogs.Where(c => c.CallStatus == "Closed" && c.ClosedDate.Substring(0, 4).Equals("2013"));
    }

    public IQueryable<CallLog> GetCallLogsLastThisYear()
    {
        return this.ObjectContext.CallLogs.Where(c => c.CallStatus == "Closed" && (c.ClosedDate.Substring(0, 4).Equals("2012") || c.ClosedDate.Substring(0, 4).Equals("2013")));
    }

    public IQueryable<CustomCallLog> GetCustomCallLogs(string year)
    {
        var result = from i in this.ObjectContext.CallLogs  
        join p in this.ObjectContext.Assignees on i.ClosedBy equals p.LoginID  
        where i.CallStatus == "Closed" && i.ClosedDate.Substring(0, 4) == year
        select new CustomCallLog
        { 
            Score = 
            CallLog = i.CallID, 
            Name = p.Assignee1, 
            Yr = year,
            Mth = i.ClosedDate.Substring(5, 2), 
            GroupName = p.GroupName
        };
        return result;
    }


    public IQueryable<CustomClosedJobs> GetCustomClosedJobs()
    {
        var result = from i in this.ObjectContext.CallLogs
                     where i.CallStatus == "Closed" && i.ClosedDate.Substring(0, 4) =="2012"
                     group i by i.ClosedDate.Substring(5,2) into y
                     select new CustomClosedJobs
                     {
                         Type = "heat",
                         ClosedCalls = y.Count(),
                          Mth =y.Key
                     };
        return result;
    }


    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Subsets' query.
    public IQueryable<Subset> GetSubsets()
    {
        return this.ObjectContext.Subsets;
    }
}

 public class HEATDomainService : LinqToEntitiesDomainService<FEEDBACKPRDEntities1>
{

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'qryStoringLogs' query.
    public IQueryable<qryStoringLog> GetQryStoringLogs()
    {
        return this.ObjectContext.qryStoringLogs.OrderBy(e => e.monthno);
    }

    public IQueryable<tblStoringLog> GetTop100Logs()
    {
        return this.ObjectContext.tblStoringLogs.OrderByDescending(e => e.responsetime).Take(100);
    }

    public IQueryable<tblStoringLog> GetLogCount(DateTime s, DateTime e)
    {
        return this.ObjectContext.tblStoringLogs.Where(x => x.responsetime >= s &&  x.responsetime <= e);
    }

    public IQueryable<qryStoringLog> GetLogs(int year, int mth)
    {
        return this.ObjectContext.qryStoringLogs.Where(e => e.Month.Equals(mth) && e.yr.Equals(year));
    }

    public IQueryable<qryStoringLog> GetLogsForYear(int year)
    {
        return this.ObjectContext.qryStoringLogs.Where(e => e.yr.Equals(year)).OrderBy(e => e.monthno);
    }

    public DateTime GetFirstDate()
    {
        return (DateTime)this.ObjectContext.tblStoringLogs.OrderBy(e => e.responsetime).First().responsetime;
    }

    public DateTime GetLastDate()
    {
        return (DateTime)this.ObjectContext.tblStoringLogs.OrderByDescending(e => e.responsetime).First().responsetime;
    }

    public IQueryable<tblCallLogged> GetTblCallLoggeds()
    {
        return this.ObjectContext.tblCallLoggeds;
    }

    public IQueryable<tblStoringLog> GetTblStoringLogs()
    {
        return this.ObjectContext.tblStoringLogs;
    }

    [Query(IsComposable = false)]
    public IQueryable<qryStoringLog> GetTblStoringLogsStatus(int statusid)
    {
        return this.ObjectContext.qryStoringLogs.Where(e => e.statusid == statusid);
    }


    public IQueryable<stResponsesLife_Result> LifeTimeResponses()
    {
        return this.ObjectContext.stResponsesLife().AsQueryable();
    }


    public IEnumerable<CustomLifetime> GetCustomLifeTime()
    {
        var result = from i in this.ObjectContext.stResponsesLife().ToList()
                     select new CustomLifetime
                     {
                         Dates = i.Dates,
                         Vals = (int)i.Vals
                     };
        return result;
    }


    public int GetAllResponses()
    {
        return this.ObjectContext.qryStoringLogs.Count();
    }

}

注意事项: 无法链接服务器,因此在源(SQL Server)上执行此操作是不可能的。 无法创建SP并使用OpenQuery(我可以,但我想学会正确地做),因为我确定这不是正确的方法。

1 个答案:

答案 0 :(得分:0)

您可能需要第三种类型来包装从每个服务返回的两种不同类型,但这取决于您希望如何使用它。

ItemsControl不关心集合中的类型(Listbox,combobox等),但GridView类型控件可能很挑剔。

使用linq,两个列表都可以简单地合并。像下面这样的东西可以做到这一点:

collection1.Select(o => (object)o).Concat(collection2.Select(o => (object)o));

选择和转换是为了通过查询创建适当的通用集合。

可以调整此选项以将转换合并到您的包装类型中。 ie而不是强制转换为Object,只需返回包装类的新实例。

如果合适,您甚至可以使用联盟。

全部放在一起:

collection1.Select(o => new MyWrapperType(o))
    .Union(collection2.Select(o => new MyWrapperType(o)));