RelationalQueryableExtensions.FromSql Method

Definition

Overloads

FromSql<TEntity>(DbSet<TEntity>, FormattableString)

Creates a LINQ query based on an interpolated string representing a SQL query.

FromSql<TEntity>(IQueryable<TEntity>, FormattableString)
Obsolete.

Creates a LINQ query based on an interpolated string representing a 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 interpolated parameter place holders in the SQL query string. Any interpolated parameter values you supply will automatically be converted to a DbParameter - context.Blogs.FromSql($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})").

FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])
Obsolete.

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).

This overload also accepts DbParameter instances as parameter values. This allows you to use named parameters in the SQL query string - context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

FromSql<TEntity>(IQueryable<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))

FromSql<TEntity>(DbSet<TEntity>, FormattableString)

Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs

Creates a LINQ query based on an interpolated string representing a SQL query.

public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this Microsoft.EntityFrameworkCore.DbSet<TEntity> source, FormattableString sql) where TEntity : class;
static member FromSql : Microsoft.EntityFrameworkCore.DbSet<'Entity (requires 'Entity : null)> * FormattableString -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As DbSet(Of TEntity), sql As FormattableString) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
DbSet<TEntity>

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

sql
FormattableString

The interpolated string representing a SQL query with parameters.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the interpolated string SQL query.

Remarks

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

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 interpolated parameter place holders in the SQL query string. Any interpolated parameter values you supply will automatically be converted to a DbParameter.

See Executing raw SQL commands with EF Core for more information and examples.

Applies to

FromSql<TEntity>(IQueryable<TEntity>, FormattableString)

Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs

Caution

For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.

Creates a LINQ query based on an interpolated string representing a 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 interpolated parameter place holders in the SQL query string. Any interpolated parameter values you supply will automatically be converted to a DbParameter - context.Blogs.FromSql($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})").

public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, FormattableString sql) where TEntity : class;
[System.Obsolete("For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.", true)]
public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, FormattableString sql) where TEntity : class;
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * FormattableString -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
[<System.Obsolete("For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.", true)>]
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * FormattableString -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As IQueryable(Of TEntity), sql As FormattableString) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
IQueryable<TEntity>

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

sql
FormattableString

The interpolated string representing a SQL query.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the interpolated string SQL query.

Attributes

Applies to

FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])

Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs
Source:
RelationalQueryableExtensions.cs

Caution

For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.

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).

This overload also accepts DbParameter instances as parameter values. 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 System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, Microsoft.EntityFrameworkCore.RawSqlString sql, params object[] parameters) where TEntity : class;
[System.Obsolete("For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.", true)]
public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, Microsoft.EntityFrameworkCore.RawSqlString sql, params object[] parameters) where TEntity : class;
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * Microsoft.EntityFrameworkCore.RawSqlString * obj[] -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
[<System.Obsolete("For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.", true)>]
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * Microsoft.EntityFrameworkCore.RawSqlString * obj[] -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As IQueryable(Of TEntity), sql As RawSqlString, ParamArray parameters As Object()) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
IQueryable<TEntity>

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

sql
RawSqlString

The raw SQL query. NB. A string literal may be passed here because RawSqlString is implicitly convertible to string.

parameters
Object[]

The values to be assigned to parameters.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the raw SQL query.

Attributes

Applies to

FromSql<TEntity>(IQueryable<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 System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, string sql, params object[] parameters) where TEntity : class;
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * string * obj[] -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As IQueryable(Of TEntity), sql As String, ParamArray parameters As Object()) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
IQueryable<TEntity>

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

sql
String

The raw SQL query.

parameters
Object[]

The values to be assigned to parameters.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the raw SQL query.

Applies to