using System.Collections.Generic;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(
IsInvariantToOrder = false,
IsInvariantToNulls = false,
IsInvariantToDuplicates = false,
public struct Concat : IBinarySerialize, INullable
private StringBuilder _accumulator;
private string _delimiter;
private List<string> AccumulatorList;
_accumulator = new StringBuilder();
_delimiter = string.Empty;
AccumulatorList = new List<string>();
public void Accumulate(SqlString Value, SqlString Delimiter)
if (Delimiter.IsNull == false && Delimiter.Value.Length > 0)
_delimiter = Delimiter.Value;
if (Value.IsNull == false)
AccumulatorList.Add(Value.Value);
public void Merge(Concat Group)
if (Group.AccumulatorList != null)
AccumulatorList.AddRange(Group.AccumulatorList);
public SqlString Terminate()
return IsNull ? SqlString.Null : new SqlString(string.Join(_delimiter, AccumulatorList.Distinct()));
void IBinarySerialize.Read(System.IO.BinaryReader r)
IsNull = r.ReadBoolean();
_delimiter = r.ReadString();
AccumulatorList = new List<string>();
AccumulatorList.Add(r.ReadString().Trim());
if (AccumulatorList.Count != 0)
void IBinarySerialize.Write(System.IO.BinaryWriter w)
w.Write(string.Join(_delimiter, AccumulatorList.Distinct()));
public Boolean IsNull { get; private set; }