Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7063447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:42:52+00:00 2026-05-28T04:42:52+00:00

I have setup a test database and console app to confirm the following: Given

  • 0

I have setup a test database and console app to confirm the following:

Given a SQL Database with the following function:

CREATE FUNCTION ufn_GTFO
(
    @Guid as uniqueidentifier
)
RETURNS VARCHAR(100)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @Result as VARCHAR(100)

    -- Add the T-SQL statements to compute the return value here
    SELECT @Result = 'This is a test'

    -- Return the result of the function
    RETURN @Result
END
GO

And the following table:

CREATE TABLE [dbo].[Test](
    [PKey] [int] IDENTITY(1,1) NOT NULL,
    [WFT] [uniqueidentifier] NULL,
 CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED 
(
    [PKey] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

The Scalar valued function ufn_GTFO is normally composable, such that the following C#:

static void ConcreteTest()
{
    DataClasses1DataContext db = new DataClasses1DataContext();
    var q = from t in db.Tests
            select new { t.PKey, GTFO = db.ufn_GTFO(t.WFT) };

    var p = q.ToArray();
}

Is translated into the following SQL:

SELECT [t0].[PKey], [dbo].[ufn_GTFO]([t0].[WFT]) AS [GTFO] FROM [dbo].[Test] AS [t0]

However, if I use the refactor -> extract interface on the DataContext, and use an instance of that:

static void InterfaceTest()
{
    IDataClasses1DataContext db = new DataClasses1DataContext();
    var q = from t in db.Tests
            select new { t.PKey, GTFO = db.ufn_GTFO(t.WFT) };

    var p = q.ToArray();
}

I get the following SQL, and calls to ufn_GTFO occur once for each record as .ToArray() enumerates the results.

SELECT [t0].[PKey], [t0].[WFT] AS [guid]
FROM [dbo].[Test] AS [t0]

So, my question is why does this happen and what can I do to prevent it while still using the interface?

Update 1: I’ve compared the IL generated for the concrete method versus the interface method, and they differ only in the reference to the interface and a compiler generated display class that doesn’t seem to have any bearing on the result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:42:53+00:00Added an answer on May 28, 2026 at 4:42 am

    linq to sql relies heavily on attributes to map class (datacontext) members to database members. Your interfaces likely does not have the FunctionAttribute over the ufn_GRFO method. without that attribute the link between the C# world and the SQL function is severed; however. your interface is also not decorated with the DatabaseAttribute as that attribute is only valid on classes and not interfaces. without that attribute you have severed the link between C# and the whole database. by default Linq uses the AttributeMappingSource to map type members in the DataContext. Sense the interface is the type in question and that type has severed the link to that database, due to the fact that you can’t apply the database attribute to it, your default mapping source will not map the function ufn_GRFO to the database function, rather linq will treat it as a .NET function to call with the data from the WFT field.

    I suspect the way around this issue is to provide your datacontext with a custom MappingSource implementation and that ignores the DatabaseAttribute and only considers the attributes on the properties.

    http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.functionattribute.aspx

    http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.databaseattribute.aspx

    http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.attributemappingsource.aspx

    http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.mappingsource.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
I have a database that's setup in the following way id coach team event
I have an established SQL Server database setup. I then generated an Entity Framework
I've created some MbUnit Test Fixtures that have SetUp methods marked with the SetUp
I want to have a good test setup but, I can't find a way
I have a ubr file setup to stress test an internal DotNetNuke site with
I have a checkstyle suppression filter setup (e.g. ignore magic numbers in unit test
Suppose I have several small test cases. Easy enough to setup and easy enough
I'm stress testing a web app and have set up a windows test program
I have the following django test case that is giving me errors: class MyTesting(unittest.TestCase):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.