至於繼續this question的時候,我有這樣的方法:異常調用ToList()
public IEnumerable<Splitting> Get(Guid companyId, long customerId)
{
CustomerRepository customersRep = new CustomerRepository();
var customers = customersRep.Get(companyId);
return GetQuery().Join(customers,
s => s.CustomerId,
c => c.Id,
(s, c) => new { s, c }).
Where(sc => sc.c.Id == customerId).
Select(sc => sc.s);
}
當我做:
var query=Get(someGuid, someCustomerId);
query.ToList(); //This throws an exception
我得到異常:
Unable to create a constant value of type 'MyProj.Domain.Business.Entities.Company.Customers.Customer'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
這種例外情況如何,我該如何解決?
UPDATE:
GetQuery()
返回Context.CreateObjectSet<T>()
其中T在這種情況下是Splitting
類。
CustomerRepository.Get(companyId)
是:
public IEnumerable<Customer> Get(Guid companyId)
{
return GetQuery().
Where(x => x.CompanyId == companyId).
Where(x => x.IsDeleted == false).
AsEnumerable().
OrderBy(x => x.Name.IsNumeric()).
ThenBy(x => x.Name);
}
什麼是'GetQuery()'?你可以顯示'GetQuery'和'CustomerRepository.Get'的代碼嗎? – 2011-06-03 02:28:14
@Alex Aza:我更新了問題。 – Naor 2011-06-03 15:37:30
這與'ToList()'沒有任何關係。如果你使用'Count()'或者迭代一個'foreach',你會得到相同的異常。 – 2011-06-03 18:38:32