C# – How to use UDF output as LINQ to SQL entity class property value

cdesignerlinq-to-sqluser-defined-functions

Just starting out with LINQ to SQL (you can probably tell – be gentle).

I'd like to use the value returned by a user-defined function as an entity class property value and have the the value populated when all the other basic (column) fields are loaded.

I know there are ways to use the UDF from a L2S query (here, and here), but I'd prefer to have the value automatically loaded.

For example, I'd expect LINQ to SQL to generate SQL similar to this and populate a property on my User entity class called AssociatedRecordCount with the integer value returned by GetAssociatedRecordCount:

SELECT ID, dbo.GetAssociatedRecordCount(ID) As AssociatedRecordCount
FROM Users

Also, I'm using the Visual Studio LINQ to SQL designer.

Thank you!

Best Answer

Try to create a new custom property not associated with the database and call the function (added to the context) in the getter.