4

我有以下代码:

IQueryable<SomeType> allItems = ...;
var selectedItems = allItems.Where( item => ( item.State == 1 || item.State == 3 ) );
Console.WriteLine( selectedItems.ToString() );

我得到以下查询:

SELECT [t0].[ItemId], [t0].[State], <other columns>
FROM [Items] AS [t0]
WHERE [t0].[State] = @p0 OR [t0].[State] = @p1

您会看到,实际值 1 和 3 没有插入到查询中,而是作为参数传递,这通常会导致选择次优的执行计划。

我可以以某种方式使 Ling-To-Sql 发射WHERE [t0].[State] = 1 OR [t0].[State] = 3吗?

4

1 回答 1

-1

There are couple of ways to manipulate the sql once Linq to sql has generated it as described here and here, but I would not recommend using either of them as they will likely be stringy and brittle.

Paramaterized sql is one of the key benefits of using an orm like linq to sql or entity framework, this prevents sql injection. Sql server will also cache the execution plans for paramaterized sql resulting in increased overall application performance.

This is a good article on the benefits of paramaterized sql.

于 2014-10-31T15:58:16.483 回答