RelationalQueryableExtensions RelationalQueryableExtensions Class

Definition

Relational database specific extension methods for LINQ queries.

public static class RelationalQueryableExtensions
Public Module RelationalQueryableExtensions
Inheritance
System.ObjectSystem.Object
RelationalQueryableExtensionsRelationalQueryableExtensions

Methods

Equals(Object) Equals(Object)

Inherited from System.Object

Equals(Object, Object) Equals(Object, Object)

Inherited from System.Object

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

The type of the elements of source.

Parameters
source
System.Linq.IQueryable<TEntity> System.Linq.IQueryable(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<TEntity> System.Linq.IQueryable(Of TEntity)

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

GetHashCode() GetHashCode()

Inherited from System.Object

GetType() GetType()

Inherited from System.Object

MemberwiseClone() MemberwiseClone()

Inherited from System.Object

ReferenceEquals(Object, Object) ReferenceEquals(Object, Object)

Inherited from System.Object

ToString() ToString()

Inherited from System.Object