Count SQL statement generated by entity framework 4

Source: Internet
Author: User

In ADO. NET Entity Framework 4, the SQL statement generated by the LINQ to entities statement that queries the number of records actually contains subqueries.

>>> Query code using Entity Framework 4:

 using (SpaceObjectContext context = new SpaceObjectContext())
{
return context.SiteMsgs.Count(msg => msg.SenderSpaceUserId == 1);
}

>>> Generated SQL statement:

 SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[club_Message] AS [Extent1]
WHERE 1 = [Extent1].[SenderID]
) AS [GroupBy1]

However, this problem does not exist in LINQ to SQL:

>>> Query using LINQ to SQL:

using(club_CNBlogsDataContext context = new club_CNBlogsDataContext())
{
return context.club_Messages.Count(msg => msg.SenderID == 1);
}

>>> Generated SQL statement:

 exec sp_executesql N'SELECT COUNT(*) AS [value]
FROM [dbo].[club_Message] AS [t0]
WHERE [t0].[SenderID] = @p0',N'@p0 int',@p0=1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.