简体   繁体   English

在两个表之间使用LINQ加入,只从其中一个表返回记录

[英]Join using LINQ between two tables, return records from only one of them

Here's my code: 这是我的代码:

public IEnumerable<IUIUPCItemShort> GetActiveWithDefaultCost()
    {
        var query = from upc in DataManagerFactory.Get().Manage<IUPCItem>().GetAll()
                    join inv in DataManagerFactory.Get().Manage<IInventory>().GetAll() on upc.UPCID equals inv.UPCID
                    where inv.ExitDate == null
                         && upc.UnitCost == null
                    select upc;
        return
            query.Cast<IUIUPCItemShort>().ToList();

Basically, I'm trying to do a Join between two tables, UPC and Inventory , and would like to have a list of only UPC that satisfy the WHERE conditions since I only want to show the user a list of UPC . 基本上,我正在尝试在两个表, UPCInventory ,并且希望只有一个满足WHERE条件的UPC列表,因为我只想向用户显示UPC列表。 I obviously am doing something really wrong, because I get this message: 我显然做了一些非常错误的事情,因为我得到了这样的信息:

Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'SubSonic.Linq.Structure.ProjectionExpression'. 无法将类型为“System.Linq.Expressions.MethodCallExpressionN”的对象强制转换为“SubSonic.Linq.Structure.ProjectionExpression”。

I assume the problem is in my Linq code, especifically in the select upc; 我认为问题出在我的Linq代码中,特别是在select upc; How can I do what I'm trying to accomplish? 我该怎样做我想要完成的事情?

Thanks. 谢谢。

Your query is a query of IUPCItem items, not of IUIUPCItemShort items. 您的查询是IUPCItem项的查询,而不是IUIUPCItemShort项的查询。 It would appear that they query provider doesn't know how to translate the Cast operation for that conversion. 看起来他们的查询提供程序似乎不知道如何转换该转换的Cast操作。

If you know that IUPCItem in your C# code is convertible to a IUIUPCItemShort then iyou probably just want to avoid having the query provider try to process the Cast . 如果你知道IUPCItem在你的C#代码转换为一个IUIUPCItemShort然后iyou可能只是想避免的查询提供尝试处理的Cast Ensure that it's done in Linq to Objects like so: 确保它在Linq to Objects中完成,如下所示:

return query.AsEnumerable().
    Cast<IUIUPCItemShort>()
    .ToList();

If that doesn't work then it means the type really isn't implicitly convertible; 如果这不起作用,则意味着该类型实际上不可隐式转换; you need to determine how to properly get an instance of IUIUPCItemShort from a IUPCItem , possibly creating a method to handle the conversion that you call using Select . 您需要确定如何从IUPCItem正确获取IUIUPCItemShort的实例,可能创建一个方法来处理您使用Select调用的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM