我有这段代码
IQueryable<Dealer> dealers =
from dealer in Dealers
join address in AddressesUS on dealer.DealerId equals address.OwnerId
where dealer.Country == country && address.Owner == (int)Contact.OwnerType.Dealer
select new Dealer()
{
DealerId = dealer.DealerId,
DealerName = dealer.DealerName,
Country = dealer.Country,
Email = dealer.Email,
Contact = dealer.Contact,
Url = dealer.Url,
IsActive = dealer.IsActive,
IsWholesale = dealer.IsWholesale,
Address = address == null ? null : address
};
当我执行它时(通过调用ToArray或其他),我收到此错误:System.InvalidCastException:从'System.String'到'***。Model.Address'的无效转换
现在,如果我将Dealer.Address更改为类型字符串,则代码无法按预期编译,因为它应该是AddressUS:Address类型。但是,如果我将Dealer.Address的类型更改为object并运行代码,我会看到SubSonic使用DB中的第一个字符串列填充它。 I.E. dealer.Address =“123假圣” DB对于经销商和地址没有任何关系,所以我不确定这是否会产生影响。
我正在使用.Net 3.5和MySql 5.
这是堆栈跟踪
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at SubSonic.Extensions.Objects.ChangeTypeTo(Object value, Type conversionType)
at SubSonic.Extensions.Database.Load[T](IDataReader rdr, T item, List`1 ColumnNames)
at SubSonic.Extensions.Database.ToEnumerable[T](IDataReader rdr, List`1 ColumnNames)
at SubSonic.Linq.Structure.DbQueryProvider.Execute[T](QueryCommand`1 query, Object[] paramValues)
at lambda_method(ExecutionScope )
at SubSonic.Linq.Structure.DbQueryProvider.Execute(Expression expression)
at SubSonic.Linq.Structure.QueryProvider.System.Linq.IQueryProvider.Execute(Expression expression)
at SubSonic.Linq.Structure.Query`1.GetEnumerator()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at InSite.Controllers.DealerController.GetDealers() in ***Controllers\DealerController.cs:line 25
at lambda_method(ExecutionScope , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
答案 0 :(得分:0)
Subsonic中有一个错误...有关详细信息,请参阅此Linq and SubSonic - returning nested complex types。
您可以通过返回匿名类型而不是经销商,或者在选择投影之前调用ToList()来验证是否存在同样的问题。如果它适用于那些情况,那么它就是同样的错误。