Linq Join 的问题(亚音速) [英] Issue with Linq Join (subsonic)

查看:30
本文介绍了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: Invalid cast from 'System.String' to '***.Model.Address'

When I execute it (by calling ToArray or whatever), I get this error: System.InvalidCastException: Invalid cast from 'System.String' to '***.Model.Address'

现在,如果我将 Dealer.Address 更改为 string 类型,代码将无法按预期编译,因为它实际上应该是 AddressUS:Address 类型.但是,如果我将 Dealer.Address 的类型更改为 object 并运行代码,我会看到 SubSonic 用来自 DB 的第一个字符串列填充它.IE.Dealer.Address = "123 Fake St."经销商和地址在 DB 中没有关系,所以我不确定这是否会有所作为.

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.

我使用的是 .Net 3.5 和 MySql 5.

I am using .Net 3.5 and 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)

推荐答案

Subsonic 中存在一个错误...请参阅 Linq 和 SubSonic - 返回嵌套的复杂类型 以获取更多信息.

There's a bug in Subsonic ... see this Linq and SubSonic - returning nested complex types for more information.

您可以通过返回匿名类型而不是 Dealer 或通过在 Select 投影之前调用 ToList() 来验证是否存在相同的问题.如果它在这些情况下有效,那么它就是相同的错误.

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.

这篇关于Linq Join 的问题(亚音速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆