2010-08-21 49 views
1

我的代码问题LINQ的加入(亚音速)

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:从“系统无效转换。字符串'到'***。Model.Address'

现在,如果我将Dealer.Address更改为键入字符串,则代码无法按我的预期进行编译,因为它确实应该是AddressUS:Address类型。但是,如果我将Dealer.Address的类型更改为对象并运行代码,我会看到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

“address == null”是什么? null:地址与“地址”不同? – Gabe 2010-08-21 02:28:49

+0

它不是,这是从我尝试做address = address == null时遗留的代码? null:new AddressUS {...} – Peijen 2010-08-23 16:32:05

回答

0

有亚音速一个bug ......看到这Linq and SubSonic - returning nested complex types以获取更多信息。

您可以通过返回匿名类型而不是Dealer或通过在选择投影之前调用ToList()来验证它是同一问题。如果它在这些情况下起作用,那么它就是同样的错误。

+0

是的,当我使用匿名类型时它肯定有效。我很确定这是一个bug,不幸的是我有截止日期,否则我会下载源代码,看看我能否修复它。 – Peijen 2010-08-23 16:36:23

+0

你可以把它作为问题记录下来,或者添加一些细节,如果它已经登录github http://github.com/subsonic/SubSonic-3.0/issues – 2010-08-23 18:54:35