简体   繁体   English

与ServiceStack.OrmLite的左连接返回一个空对象而不是null

[英]Left join with ServiceStack.OrmLite returns an empty object instead of null

I've created a repository with a complete example of what I'm trying to do . 我已经创建了一个存储库,其中包含我正在尝试做的完整示例

I have the following schema: 我有以下架构:

class Order
{
    public int OrderId { get; set; }
}

class LineItem
{
    public int LineItemId { get; set; }
    public int OrderId { get; set; }
}

I'm using ServiceStack.OrmLite to left join Order with LineItem, using this code: 我正在使用ServiceStack.OrmLite将Connect与LineItem连接起来,使用以下代码:

var query = db.From<Order>()
              .LeftJoin<LineItem>()
              .Where(o => o.OrderId == 1);

var results = db.SelectMulti<Order, LineItem>(query);

SelectMulti() returns a List<Tuple<Order, LineItem>> . SelectMulti()返回List<Tuple<Order, LineItem>> When an order has no line items, I'm getting back new LineItem() instead of null . 当订单没有订单项时,我将返回new LineItem()而不是null

I expected to get null back so I could tell the difference between "no line items exist for this order" and "this order has a line item with default values". 我希望得到null所以我可以区分“此订单没有订单项”和“此订单有一个默认值的订单项”。

I could check for the line item's OrderId being equal to the order's OrderId , but in theory I could have an order with OrderId 0, so in that case I wouldn't be able to tell. 可以检查订单项的OrderId是否等于订单的OrderId ,但理论上我可以订购OrderId 0,所以在这种情况下我无法分辨。

Is there a better way to do this left join with OrmLite? 有没有更好的方法与OrmLite进行左联接?

The quick and dirty solution I used was to check whether the related objects returned were "empty". 我使用的快速而肮脏的解决方案是检查返回的相关对象是否为“空”。

I did that with a small inline function as follows: 我用一个小的内联函数做了如下:

bool IsEmptyObject<T>(T instance)
{
    var empty = Activator.CreateInstance(instance.GetType());
    return empty.ToJson() == instance.ToJson();
}

I expected to get null back so I could tell the difference between "no line items exist for this order" and "this order has a line item with default values". 我希望得到null,所以我可以区分“此订单没有订单项”和“此订单有一个默认值的订单项”。

The issue with LEFT JOIN is that the row does exist but all fields are returned as null which OrmLite maps to an instance where no fields are initialized which is indistinguishable from a row that doesn't exist. LEFT JOIN的问题是该行确实存在,但所有字段都返回为null ,OrmLite映射到没有初始化字段的实例,这与不存在的行无法区分。

I could check for the line item's OrderId being equal to the order's OrderId, but in theory I could have an order with OrderId 0, so in that case I wouldn't be able to tell. 我可以检查订单项的OrderId是否等于订单的OrderId,但理论上我可以订购OrderId 0,所以在这种情况下我无法分辨。

This should not be possible as Auto Incrementing Primary Keys start at 1 (by default) and increment upwards. 这不应该是可能的,因为自动递增主键从1开始(默认情况下)并向上递增。 You should consider it invalid state (which is likely an indication it was added incorrectly) if you have an int primary key with 0 so checking against default(int) should suffice. 如果你有一个0int主键,你应该认为它是无效状态(这可能表明它被错误地添加),因此检查default(int)就足够了。

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

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