1

当我尝试这个时:

//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;
}

没有例外。

有什么建议吗?

谢谢。

4

1 回答 1

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();

另外,我想知道您是如何实现“Company”和“CompanyPerson”类的。你能展示那些类的源代码吗?

于 2012-05-26T16:23:40.807 回答