简体   繁体   English

查找 Dbset 中是否存在对象

[英]Find if Object Exists in Dbset

I have a DbSet object DbSet<ShippingInformation> ShippingInformations;我有一个DbSet对象DbSet<ShippingInformation> ShippingInformations; I have also overridden the equals operator for ShippingInformation.我还覆盖了 ShippingInformation 的equals运算符。

How can I find if there is an existing object, y in the set ShippingInformations that is equal to object x, both of type ShippingInformation .我怎样才能找到一个现有的对象, y 在等于对象 x 的集合ShippingInformations中,两者都是ShippingInformation类型。

So far I have tried:到目前为止,我已经尝试过:

storeDB.ShippingInformations.Contains(shippingInformation);

However, that only works for primitive types.但是,这只适用于原始类型。

Unfortunately you can't use your Equals implementation in a query to EF because it can't decompile your code to see how it's done.不幸的是,您不能在对 EF 的查询中使用您的Equals实现,因为它无法反编译您的代码以查看它是如何完成的。 You should be fine using Any method with a predicate expression:您应该可以使用带有谓词表达式的Any方法:

bool exists = storeDB.ShippingInformations
    .Any(info =>
            info.CustomerID == other.CustomerID
            && info.CountryID == other.CountryID
        );

(I made the fields up just to show the idea, other is the ShippingInformation you're looking for.) (我只是为了展示这个想法而制作了这些字段, other是您正在寻找的ShippingInformation 。)

If there are many places where you want to re-use this expression, you might want to use LinqKit to combine expressions:如果有很多地方要重用这个表达式,你可能想用LinqKit来组合表达式:

private static Expression<Func<ShippingInformation, ShippingInformation, bool>>
    isEqualExpr =
        (info, other) =>
            info.CustomerID == other.CustomerID
            && info.CountryID == other.CountryID;


// somewhere down this class

var expr = isEqualExpr; // reference the expression locally (required for LinqKit)
bool exists = storeDB.ShippingInformations
                  .Any(x => expr.Invoke(x, other)); // "injects" equality expression

Such code should be placed in data layer.这样的代码应该放在数据层。

I'm not 100% sure if the above code works though.我不是 100% 确定上面的代码是否有效。 It may very well be that EF won't allow “other” object to be used in the query expression. EF 很可能不允许在查询表达式中使用“其他”对象。 If this is the case (please let me know), you'll have to modify the expression to accept all primitive type values for comparison (in our example, it would've become Expression<Func<ShippingInformation, int, int, bool>> ).如果是这种情况(请告诉我),您必须修改表达式以接受所有原始类型值进行比较(在我们的示例中,它会变成Expression<Func<ShippingInformation, int, int, bool>> )。

bool ifExists = storeDB.ShippingInformations.Any(shi=>shi.Id == objectYouWantToCompareTo.Id);

或者,如果您覆盖 equals 运算符,这也应该有效。

bool ifExists = storeDB.ShippingInformations.Any(shi=>shi == objectYouWantToCompareTo);

try this:试试这个:

storeDB.ShippingInformations.ToList().Contains(shippingInformation);

you may also have to implement a IEqualityComparer to get what you're looking for.您可能还必须实现IEqualityComparer才能获得所需的内容。

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

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