Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to figure out how to get this sql equivelant:

SQL
select checksum_agg(checksum(*)) from Foo



From linq SqlFunctions. I'm able to easily get the checksum for one column, so long as I know the column. However, I won't know column names. So I can do something like:

C#
SqlFunctions.ChecksumAggregate(Foo.Select(x => x.Id))



But that is only the checksum of one column, and I have to know the column name. Is there a way to checksum the collection like the SQL statement? Or to get the checksum a column at a time without knowing the column name?
Posted
Updated 30-Jul-13 4:26am
v2

1 solution

You can use ChecksumAggregate method for this.

It's like this :

C#
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    // SqlFunctions.ChecksumAggregate is executed in the database.
    decimal? checkSum = SqlFunctions.ChecksumAggregate(
        from o in AWEntities.SalesOrderHeaders
        select o.SalesOrderID);

    Console.WriteLine(checkSum);
}


For more info check this :

http://msdn.microsoft.com/en-us/library/vstudio/dd456858(v=vs.100).aspx[^]

http://msdn.microsoft.com/en-us/library/vstudio/dd487151(v=vs.100).aspx[^]

I hope this will help to you.
 
Share this answer
 
v2
Comments
loctrice 30-Jul-13 10:27am    
I have figured that much out. The problem I am running into is how to do that over the whole linq collection. Or, if doing it one column at a time, to be able to do it without knowing the column name and type for the checksum.
Sampath Lokuge 30-Jul-13 10:30am    
What happened when you do like this :decimal? checkSum = SqlFunctions.ChecksumAggregate(
from o in AWEntities.SalesOrderHeaders
select o.*); In other words * instead of column name.
loctrice 30-Jul-13 10:48am    
Linqpad gives me an invalid expression error. I don't think you can use .* in c#
Sampath Lokuge 30-Jul-13 10:52am    
I am sorry.Just try like this : decimal? checkSum = SqlFunctions.ChecksumAggregate(
from o in AWEntities.SalesOrderHeaders
select o);
loctrice 30-Jul-13 10:59am    
ChecksumAggregate takes IEnumerable of int so that one won't work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900