从IQueryable创建ObservableCollection

时间:2012-05-26 16:01:12

标签: c#

我试试这个:

//IRepository.GetAll<CompanyPerson> returns an IQueriable
//ToObservableList() extension method converts to ObservableCollection
 var tt =
        (from s in container.Resolve<IRepository>().GetAll<Company>()
         join t in container.Resolve<IRepository>().GetAll<Personels>().ToObservableList()
         on s.ID equals t.CompanyID into k
         from subset in k
         select new { s, t = k }).ToList();

我得到了这个例外:

Unable to create a constant value of type 'CompanyPerson'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

当我尝试这个时:

     var tt =
        (from s in container.Resolve<IRepository>().GetAll<Company>()
         join t in container.Resolve<IRepository>().GetAll<Personels>()
         on s.ID equals t.CompanyID into k
         from subset in k
         select new { s, t = k }).ToList();

或者我试试这个:

(from s in container.Resolve<IRepository>().GetAll<Company>()
             join t in container.Resolve<IRepository>().GetAll<Personels>()
             on s.ID equals t.CompanyID into k
             from subset in k
             select new { s, t = new ObservableCollection<CompanyPerson>(k) }).ToList();

得到这个:

Only parameterless constructors and initializers are supported in LINQ to Entities.

ToObservableList()扩展名:

public static ObservableCollection<T> ToObservableList<T>(this IEnumerable<T> data)
{
    ObservableCollection<T> dataToReturn = new ObservableCollection<T>();

    foreach (T t in data)
        dataToReturn.Add(t);

    return dataToReturn;
}

没有例外。

任何建议?

感谢。

1 个答案:

答案 0 :(得分:0)

是的,您不能使用复杂类型进行比较(在您的示例中为t和k)

尝试指定条件:

var tt =
    (from s in container.Resolve<IRepository>().GetAll<Company>()
     join t in container.Resolve<IRepository>().GetAll<Personels>().ToObservableList()
     on s.ID equals t.CompanyID into k
     from subset in k
     select new { s, new CompanyPerson() { //here fill all properties from k variable } }).ToList();

另外,我想知道你是如何实施“公司”和“CompanyPerson”课程的。你能展示那些类的源代码吗?