Linq Join 问题(亚音速)
我有这段代码
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: Invalidcast from 'System.String' to '***.Model.Address'
现在,如果我更改Dealer.Address 类型为字符串,代码无法按我的预期编译,因为它实际上应该是 AddressUS:Address 类型。但是,如果我将 Dealer.Address 的类型更改为 object 并运行代码,我会看到 SubSonic 使用数据库中的第一个字符串列填充它。 IE 经销商.地址 = "123 Fake St."经销商和地址的数据库中没有关系,所以我不确定这是否会产生影响。
我正在使用 .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)
I have this slice of code
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
};
When I execute it (by calling ToArray or whatever), I get this error: System.InvalidCastException: Invalid cast from 'System.String' to '***.Model.Address'
Now if I change the Dealer.Address to type string, the code fail to compile as I expected becaues it should really be of type AddressUS:Address. However if I change the type of Dealer.Address to object and run the code, I see SubSonic populated it with first string column from DB. I.E. dealer.Address = "123 Fake St." There is no relation in DB for dealer and address so I am not sure if that would make a difference.
I am using .Net 3.5 and MySql 5.
Here is the stack trace
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Subsonic 中有一个错误...请参阅Linq 和 SubSonic - 返回嵌套复杂类型 了解更多信息。
您可以通过返回匿名类型(而不是 Dealer)或通过在 Select 投影之前调用 ToList() 来验证是否存在相同的问题。如果它在这些情况下有效,那么这是相同的错误。
There's a bug in Subsonic ... see this Linq and SubSonic - returning nested complex types for more information.
You can verify that it's the same problem by returning an anonymous type, instead of a Dealer, or by calling ToList() before the Select projection. If it works in those cases, then it's the same bug.