Thanks for reading this,
VS2010 against SQLServer2008 enterprise, developing CLR Aggregate function to calculate the MODE, the function is returning this error:
"Line 1 CREATE AGGREGATE failed because type 'CMode' does not conform to UDAGG specification due to field 'CS$<>9__CachedAnonymousMethodDelegate1'."
the error is produced here:
int mode = list.GroupBy(n => n).
OrderByDescending(g => g.Count()).
Select(g => g.Key).FirstOrDefault();
this is the complete code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined , MaxByteSize = 8000)]
public struct CMode : IBinarySerialize
{
private List<int> list;
public void Init()
{
this.list = new List<int>();
}
public void Accumulate(SqlInt16 Value)
{
this.list.Add(Value.Value);
}
public void Merge(CMode Group)
{
this.list.AddRange(Group.list.ToArray());
}
public SqlDecimal Terminate()
{
SqlInt16 rtn = new SqlInt16();
int mode = list.GroupBy(n => n).
OrderByDescending(g => g.Count()).
Select(g => g.Key).FirstOrDefault();
rtn = (SqlInt16)mode;
return rtn;
}
//IBinarySerialize
public void Read(BinaryReader r)
{
int itemCount = r.ReadInt16();
this.list = new List<int>(itemCount);
for (int i = 0; i <= itemCount - 1; i++)
{
this.list.Add(r.ReadInt16());
}
}
//IBinarySerialize
public void Write(BinaryWriter w)
{
w.Write(this.list.Count);
foreach (Int16 s in this.list)
{
w.Write(s);
}
}
}
Any guidance would be appreciated !!
I am able to run the wished code within a SQLCLR function what verifies that I have all the grants, the dlls are there, etc.. :
u
sing System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Linq.Expressions;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlInt16 SQLCLR2008MODE()
{
List<int> list;
list = new List<int>();
list.Add(7);
list.Add(1);
list.Add(2);
list.Add(2);
list.Add(3);
list.Add(3);
list.Add(4);
list.Add(4);
list.Add(5);
list.Add(5);
list.Add(6);
int mode = list.GroupBy(n => n).
OrderByDescending(g => g.Count()).
Select(g => g.Key).FirstOrDefault();
return (Int16)mode;
}
};
Looking forward your comments.