Answered About AsQueryable

  • Sunday, January 20, 2013 12:48 PM
     
      Has Code

    What is the purpose of "AsQueryable"?

    All I was able to glean from monkeying around with the method is that it allows to convert from "IEnumerable" back to "IQueryable" but that seems like a pointless conversion (at least I see no use for it). All the explanations I;'ve found sounded too "academic" without examples.

    I only see some use in the following example:

    dc.Customers.AsEnumerable().Where(c => c.CompanyName == "The Big Cheese").ToList();

    it runs this SQL:

    SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
    FROM [dbo].[Customers] AS [t0]

    And if we convert to "AsQueryable" then the result drastically changes:

    exec sp_executesql N'SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
    FROM [dbo].[Customers] AS [t0]
    WHERE [t0].[CompanyName] = @p0',N'@p0 nvarchar(4000)',@p0=N'The Big Cheese'
    

    But we didn't even have to convert to "IEnumerable" in the first place, so this use of "AsQueryable" seems pointless.

    Could you give me a better example, where the use of "AsQueryable" would be really indispensable?

    Thanks!

All Replies