RelationalQueryableExtensions RelationalQueryableExtensions Class

Relational database specific extension methods for LINQ queries.

Syntax

Declaration

public static class RelationalQueryableExtensionsPublic Module RelationalQueryableExtensions

Inheritance Hierarchy

  • System.Object
    System.Object
  • RelationalQueryableExtensions
    RelationalQueryableExtensions

Inherited Members

ToString(),
ToString(),
Equals(Object),
Equals(Object),
Equals(Object, Object),
Equals(Object, Object),
ReferenceEquals(Object, Object),
ReferenceEquals(Object, Object),
GetHashCode(),
GetHashCode(),
GetType(),
GetType(),
MemberwiseClone()
MemberwiseClone()

Methods summary

Creates a LINQ query based on a raw SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter -

context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)
.

You can also construct a DbParameter and supply it to as a parameter value. This allows you to use named parameters in the SQL query string -

context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

Methods

  • FromSql<TEntity>(IQueryable<TEntity>, String, Object[])
    FromSql(Of TEntity)(IQueryable(Of TEntity), String, Object())

    Creates a LINQ query based on a raw SQL query.

    If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

    context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
    .

    As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter -

    context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)
    .

    You can also construct a DbParameter and supply it to as a parameter value. This allows you to use named parameters in the SQL query string -

    context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

    public static IQueryable<TEntity> FromSql<TEntity>(this IQueryable<TEntity> source, string sql, params object[] parameters)where TEntity : class<ExtensionAttribute>
    Public Shared Function FromSql(Of TEntity As Class)(source As IQueryable(Of TEntity), sql As String, ParamArray parameters As Object()) As IQueryable(Of TEntity)

    Parameters

    • source
      System.Linq.IQueryable<T><TEntity>
      System.Linq.IQueryable<T>(Of TEntity)

      An System.Linq.IQueryable<T> to use as the base of the raw SQL query (typically a DbSet<TEntity>).

    • sql
      System.String
      System.String

      The raw SQL query.

    • parameters
      System.Object[]
      System.Object()

      The values to be assigned to parameters.

    Returns

    • System.Linq.IQueryable<T><TEntity>
      System.Linq.IQueryable<T>(Of TEntity)

      An System.Linq.IQueryable<T> representing the raw SQL query.

Details

Assembly

Microsoft.EntityFrameworkCore.Relational.dll