简体   繁体   English

如何使用LINQ和Entity Framework计算一行中所有列的校验和?

[英]How do I calculate a checksum on all columns in a row using LINQ and Entity Framework?

The query I am trying to execute is similar to this: 我试图执行的查询与此类似:

var checksum = from i in db.Items
    where i.Id == id
    select SqlFunctions.Checksum("*");

However, this returns the checksum value of the string "*" rather than evaluating the wildcard. 但是,这将返回字符串"*"的校验和值,而不是评估通配符。 Is there a way to calculate the checksum of all the columns instead? 有没有办法计算所有列的校验和?

Update: 更新:

var checksum = db.Database.SqlQuery<int?>("SELECT CHECKSUM(*) FROM [Catalog].[Item] WHERE Id = @p0", id);

This gives me the result I want but seems dirty. 这给了我想要的结果,但看起来很脏。 Is there a way to do this without inline SQL? 没有内联SQL有没有办法做到这一点?

This can be done with the SqlFunctions class. 这可以使用SqlFunctions类完成。 This Class allows for linq-to-entities code to include methods that are easily converted to Sql. 此类允许linq-to-entities代码包含易于转换为Sql的方法。

First of all in your current edit: Using inline SQL is not 'dirty' and is totally fine in most (if not all) cases. 首先在您当前的编辑中:使用内联SQL并不是“脏”,并且在大多数(如果不是全部)情况下都是完全正常的。 ORMs don't provide everything, especially if there isn't a good object-column mapping that exists. ORM不提供所有内容,尤其是在没有良好的对象列映射存在的情况下。 However, since you're using entity framework you might as well get aquanted with the SqlFunctions static methods. 但是,由于您正在使用实体框架,因此您可能会使用SqlFunctions静态方法。

In this case there are a lot of overloads for performing a checksum, however they must all be of the same type. 在这种情况下,执行校验和有很多重载,但它们必须都是相同的类型。 Since you didn't post what types your columns or how many you have, I don't want to recommend the wrong overload in an example for you to use. 由于您没有发布列的类型或数量,我不想在示例中推荐错误的重载供您使用。

Here are your options: 以下是您的选择:

SqlFunctions.Checksum() : SqlFunctions.Checksum()

  • bool?
  • char[]
  • DateTime?
  • DateTimeOffset?
  • Decimal?
  • double?
  • Guid?
  • TimeSpan?
  • String

All of the above have overloads to allow up to 3 parameters (of the same type). 以上所有都有重载,允许最多3个参数(相同类型)。

SqlFunctions.AggregateChecksum() : SqlFunctions.AggregateChecksum()

  • IEnumerable<int>
  • IEnumerable<int?>

If you take a look at the documentation for these functions you'll see that the parameters that you're passing are VALUES , not column names. 如果您查看这些函数的文档,您会看到您传递的参数是VALUES ,而不是列名。 So you should be using them inside of a Select() clause. 所以你应该在Select()子句中使用它们。 This is why when you passed "*" to the operation it checksummed the string containing a single asterisk instead of all columns. 这就是为什么当您向操作传递"*"时,它会校验包含单个星号而不是所有列的字符串。 Also, keep in mind that these functions cannot be called directly, and must only be used within a Linq-To-Entities query. 另外,请记住,这些函数不能直接调用,只能在Linq-To-Entities查询中使用。

Let's assume your columns named "ItemName" & "Description" are both strings, and you also want your id, which is an int : 假设你的名为“ItemName”和“Description”的列都是字符串,你也想要你的id,这是一个int

var checksum = db.Items.Where(i => i.Id == id)
                       .Select(i => SqlFunctions.Checksum(i.Id.ToString(), i.ItemName, i.Description));

Unfortunately, as you see in the above example we had to cast our int to a string. 不幸的是,正如您在上面的示例中所看到的,我们必须将int转换为字符串。 There are no overloads that allow for different typed parameters for computing a checksum, nor are there any options that allow for more than 3 parameters in the checksum function; 没有重载允许用于计算校验和的不同类型参数,也没有任何选项允许校验和函数中有3个以上的参数; however, as I mentioned above sometimes you need to do an inline SQL command and this is OK. 但是,正如我上面提到的,有时您需要执行内联SQL命令,这没关系。

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

相关问题 如何在 Entity Framework LINQ To Entities 中进行联合? - How can I do a Union all in Entity Framework LINQ To Entities? 如何使用LINQ和实体框架6进行表联接? - How do I do a table join using LINQ and entity framework 6? 如何使用LINQ和Entity Framework对总计进行分组? - How can I do a group by with totals using LINQ and Entity Framework? 实体框架 - 如何获取列? - Entity Framework - how do I get the columns? 如何在LINQ to Entity Framework中进行分组和自定义计算? - how to groupby and custom calculate in linq to Entity Framework? 如何使用Entity Framework查找所有搜索项都包含在选定列中的行 - How to find a row where all search terms are contained within selected columns using Entity Framework Linq与实体Framework使用功能选择列 - Linq with entity Framework using a function to select columns 如何使用 linq 和实体框架访问表中特定列的所有值? - How can I access all values from a specific column in tables using linq and Entity Framework? 如何使用LINQ和Entity Framework根据条件设置参数 - How do I set a parameter based on a condition using LINQ and Entity framework 如何使用LINQ for Entity Framework检查元组中的空值或列中的单个值? - How do I check for a null value in a tuple or single value in a column using LINQ for Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM