Thanks for your answer.
My goal was not really to elaborate on the randomness of newid, but if my approach on fetching random rows from the database was right, given the fact that I need them in random order from the database, as the underlying table has many entries. For my needs the limited entrophy of newid() is not an issue. I just don't know another method to get random rows from a table.
When I try to define the above solution using a parameter-less function mapping I don't get the same results. The query is different too, missing any ORDER clause at all.
Extension definition
public static class GuidExtensions {
public static Guid Random(Guid whatever) {
throw new NotImplementedException();
}
public static Guid Random() {
return Guid.NewGuid();
}
}
Code:
public void TestFilter() {
using (var adapter = new DataAccessAdapter()) {
var metaData = new LinqMetaData(adapter){
CustomFunctionMappings = new FunctionMappingStore()
};
metaData.CustomFunctionMappings.Add(new FunctionMapping(typeof(GuidExtensions), "Random", 0, "newid()"));
var query = from t in metaData.ContentItem
orderby GuidExtensions.Random()
select t;
foreach (var i in query) {
Console.WriteLine("Guid: "+ i.Guid);
}
metaData.CustomFunctionMappings.Add(new FunctionMapping(typeof(GuidExtensions), "Random", 1, "dbo.fn_newID({0})"));
query = from t in metaData.ContentItem
orderby GuidExtensions.Random(t.Guid)
select t;
foreach (var i in query) {
Console.WriteLine("Guid: " + i.Guid);
}
}
}
The first linq query creates the following sql query:
SELECT DISTINCT [LPA_L1].[id] AS [F3_0], [LPA_L1].[guid] AS [F3_1], [LPA_L1].[ID_Rating] AS [F3_2], [LPA_L1].[created] AS [F3_3], [LPA_L1].[creator] AS [F3_4], [LPA_L1].[modified] AS [F3_5], [LPA_L1].[modifier] AS [F3_6], [LPA_L1].[saleAmount] AS [F3_7], [LPA_L1].[viewCount] AS [F3_8], [LPA_L1].[salesCount] AS [F3_9], [LPA_L1].[orientation] AS [F3_10], [LPA_L1].[validFrom] AS [F3_11], [LPA_L1].[validTo] AS [F3_12], [LPA_L1].[active] AS [F3_13], [LPA_L2].[idContentItem] AS [F11_14], [LPA_L2].[minWidth] AS [F11_15], [LPA_L2].[maxWidth] AS [F11_16], [LPA_L2].[minHeight] AS [F11_17], [LPA_L2].[maxHeight] AS [F11_18], [LPA_L2].[fitMethod] AS [F11_19], [LPA_L2].[allowImageUpload] AS [F11_20], [LPA_L2].[numberPages] AS [F11_21] FROM ( [dbContent].[dbo].[tblContentItem] [LPA_L1] LEFT JOIN [dbContent].[dbo].[tblTemplate] [LPA_L2] ON [LPA_L1].[id]=[LPA_L2].[idContentItem])
The second creates the following query:
SELECT [LPA_L1].[id] AS [F3_0], [LPA_L1].[guid] AS [F3_1], [LPA_L1].[ID_Rating] AS [F3_2], [LPA_L1].[created] AS [F3_3], [LPA_L1].[creator] AS [F3_4], [LPA_L1].[modified] AS [F3_5], [LPA_L1].[modifier] AS [F3_6], [LPA_L1].[saleAmount] AS [F3_7], [LPA_L1].[viewCount] AS [F3_8], [LPA_L1].[salesCount] AS [F3_9], [LPA_L1].[orientation] AS [F3_10], [LPA_L1].[validFrom] AS [F3_11], [LPA_L1].[validTo] AS [F3_12], [LPA_L1].[active] AS [F3_13], [LPA_L2].[idContentItem] AS [F11_14], [LPA_L2].[minWidth] AS [F11_15], [LPA_L2].[maxWidth] AS [F11_16], [LPA_L2].[minHeight] AS [F11_17], [LPA_L2].[maxHeight] AS [F11_18], [LPA_L2].[fitMethod] AS [F11_19], [LPA_L2].[allowImageUpload] AS [F11_20], [LPA_L2].[numberPages] AS [F11_21] FROM ( [dbContent].[dbo].[tblContentItem] [LPA_L1] LEFT JOIN [dbContent].[dbo].[tblTemplate] [LPA_L2] ON [LPA_L1].[id]=[LPA_L2].[idContentItem]) ORDER BY dbo.fn_newID([LPA_L1].[guid]) ASC
Maybe I didn't quite understand the definition process and usage of function mappings.
Regards,
André