简体   繁体   中英

Create conditional WHERE clause on joined table in Linq and EF

Requirement:

Show a current status of orders at this point in time. This will return all orders that are NOT yet shipped AND those orders that have been shipped TODAY.

Data Structure:

OrderHeader (1) -> (many) ShippingContainerHeaders

A copy of my code below. It originally worked with the oh.CreatedOn part that is now commented out. But the requirement became refined and instead needs to consider those Shipped TODAY using the specific ship date of the joined table ShippingContainerHeader.ShipDateTimeUTC . If any one of the ship containers for that order has a ShipDateTimeUTC of Today, then include it. But what I have below won't compile and gives this error:

Error 17 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Collections.Generic.IEnumerable<OTIS.Domain.InventoryMgmt.ShippingContainerHeader>' and 'bool'

return orderHeaderRepository
    .SearchFor(oh =>
        oh.FacilityId == intFacilityId
        && oh.OrderType == OrderHeader.OrderTypes.ShipOrder.ToString()
        && (
            oh.StatusId >= (int)OrderHeader.Statuses.Shipped && oh.ShippingContainerHeaders != null
            ? oh.ShippingContainerHeaders.Where(sh => sh.ShipDateTimeUTC >= startDateTime) //(oh.CreatedOn >= startDateTime) && (oh.CreatedOn <= endDateTime)
            : oh.Id > 0
            )
        )

UPDATE

If I change the .Where to .Any , I get this error:

Cannot compare elements of type 'System.Collections.Generic.ICollection`1[[OTIS.Domain.InventoryMgmt.ShippingContainerHeader, OTIS.Domain, Version=1.5.6054.27019, Culture=neutral, PublicKeyToken=null]]'. Only primitive types, enumeration types and entity types are supported.

My Solution

Though the accepted answer is specific in that there is no way to do what I a was trying to achieve, it does not provide an alternate solution. In the end, I had to just create a UNION query to first query all order not yet shipped and another for those that shipped, and performed a union on the two queries.

oh.ShippingContainerHeaders != null
            ? oh.ShippingContainerHeaders.Where(sh => sh.ShipDateTimeUTC >= startDateTime) //(oh.CreatedOn >= startDateTime) && (oh.CreatedOn <= endDateTime)
            : oh.Id > 0

this statment is not correct:

oh.ShippingContainerHeaders .Where(sh => sh.ShipDateTimeUTC >= startDateTime) =IEnumerable<ShippingContainerHeader>()

oh.Id > 0 is bool!

read here why!

Type of conditional expression cannot be determined because there is no implicit conversion between 'class1' and 'class2' Conversions between classes are useful when you want objects of different classes to work with the same code. However, two classes that work together cannot have mutual and redundant conversions, or no implicit conversions. The types of class1 and class2 are determined independently, and the more general type is selected as the type of the conditional expression. For more information about how types are determined, see Conditional operator cannot cast implicitly. To resolve CS0173, verify that there is one and only one implicit conversion between class1 and class2, regardless of which direction the conversion is in and regardless of which class the conversion is in. For more information, see Implicit Numeric Conversions Table (C# Reference) and Conversion Operators (C# Programming Guide).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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