如何将不同视图的结果组合到IQueryable <t> </t>

时间:2012-02-16 07:40:26

标签: wpf linq

我有一个PlanItems列表绑定到telerik RadGrid并有两个视图检索结果。每个视图获取不同的数据,所以如何将数据转换为IQueryable

public class PlanItems
{
    public int Id{get; set;}   
    public string Name { get; set; }
    public string HV { get; set; }
    public string StNumber { get; set; }
    public string LDNumber { get; set; }

    public string MSId { get; set; }
    public string CreatedBy { get; set; }
    public string Type { get; set; }

}

 I have 2 different views vw_Boys ,vw_Girls and Student Entity

首先我从学生实体中获得了boys_Id和Girls_Id

    public IQuerable<PlanItems> GetAllStudents()
        {
        var Id = from v in context.Student
                  .Include("SBoy") 
                  .Include("SGirl")                    
                  where v.CreatedBy_Id == user.Id
                  select v;
        var temp = unp.Select(a=>a.SBoy.boys_Id).Distinct();
        var temp1 = unp.Select(a => a.SGirl.girls_Id).Distinct();

           var vwboys = from nv in context.vw_boys
                 where(temp.Contains(nv.SId))
                 select nv;

        var vwgirls= from nv in context.vw_girls
                        where (temp1.Contains(nv.GId))
                        select nv;

         var Boysresult = from n in vwboys
                     select new PlanItems
                     {
                         Id = n.SId,
                         Name = n.Name,
                         HV = n.HV,
                         StNumber = n.SNumber,
                         LDNumber = n.LineNumber,
                         MSId = n.MasterId,
                         CreatedBy = n.USerName,
                        Type = n.SubjectType
                     };

        var GirlsResult = from n in vwgirls
                          select new UnplannedItems
                          {
                           Id = n.GiId,
                         Name = n.Name,
                         HV = n.GV,
                         StNumber = n.SujNumber,
                         LDNumber = n.Lid,
                         MSId = n.MasterId,
                         CreatedBy = n.USerName,
                        Type = n.SubjectType
                          };  

          }
           my telerikGrid sample

     <telerik:GridViewDataColumn Header="StudentID" IsReadOnly="True"    DataMemberBinding="{Binding ID, Mdde=OneWay}" />

如何从上面的方法返回结果。我可以将结果合并为一个。

3 个答案:

答案 0 :(得分:1)

我会这样做。

像这样向你的类添加一个新属性。因为在另一种情况下你可能不得不把男孩和女孩分开:

public class PlanItems
{
    public int Id{get; set;}   
    public string Name { get; set; }
    public string HV { get; set; }
    public string StNumber { get; set; }
    public string LDNumber { get; set; }

    public string MSId { get; set; }
    public string CreatedBy { get; set; }
    public string Type { get; set; }
        //new property
    public bool IsUnplanned { get; set; }
}

使用它们之间的连接:

var result= ( 
        from n in vwboys
        select new PlanItems
        {
            Id = n.SId,
            Name = n.Name,
            HV = n.HV,
            StNumber = n.SNumber,
            LDNumber = n.LineNumber,
            MSId = n.MasterId,
            CreatedBy = n.USerName,
            Type = n.SubjectType,
            IsUnplanned=false
        }
    ).Concat
    (
        from n in vwgirls
        select new PlanItems
        {
            Id = n.GiId,
            Name = n.Name,
            HV = n.GV,
            StNumber = n.SujNumber,
            LDNumber = n.Lid,
            MSId = n.MasterId,
            CreatedBy = n.USerName,
            Type = n.SubjectType,
            IsUnplanned=true
        }
    );

希望这有帮助

答案 1 :(得分:1)

您可以使用union扩展方法结合2个不同的IQueryable:

here是一个例子

答案 2 :(得分:0)

我不是wpf的专家,但是看看解决方案,我认为你可以为男孩和女孩使用父类,然后添加到该类的列表中。

让我们说Boy和Girl类都是从Person类继承的,然后创建

List<Person> personList = new List<Person>()

并将男孩和女孩添加到此列表中。

我不确定这是否是您需要的解决方案