从LINQ到SQL生成的T-SQL缺少where子句子句、LINQ、SQL、where

2023-09-06 08:36:27 作者:逆天飞翔

我有一个DataContext对象,名为codeLookupAccessDataContext,即是通过Visual Studio中的LINQ to SQL类向导生成。我扩展了该对象的功能,使得它暴露了一些方法来LINQ的结果返回给SQL查询。下面是我所定义的方法:

I have a DataContext object, called "CodeLookupAccessDataContext", that was generated through the Visual Studio LINQ to SQL class wizard. I extended the functionality of this object such that it exposes some methods to return results of LINQ to SQL queries. Here are the methods I have defined:

public List<CompositeSIDMap> lookupCompositeSIDMap(int regionId, int marketId)
{
    var sidGroupId = CompositeSIDGroupMaps.Where(x => x.RegionID.Equals(regionId) && x.MarketID.Equals(marketId))
        .Select(x => x.CompositeSIDGroup);

    IEnumerator<int> sidGroupIdEnum = sidGroupId.GetEnumerator();

    if (sidGroupIdEnum.MoveNext())
        return lookupCodeInfo<CompositeSIDMap, CompositeSIDMap>(x => x.CompositeSIDGroup.Equals(sidGroupIdEnum.Current), x => x);
    else
        return null;
}

private List<TResult> lookupCodeInfo<T, TResult>(Func<T, bool> compLambda, Func<T, TResult> selectLambda)
    where T : class
{
    System.Data.Linq.Table<T> dataTable = this.GetTable<T>();

    var codeQueryResult = dataTable.Where(compLambda)
        .Select(selectLambda);

    List<TResult> codeList = new List<TResult>();
    foreach (TResult row in codeQueryResult)
        codeList.Add(row);

    return codeList;
}

CompositeSIDGroupMap和CompositeSIDMap两个表中我们的数据库正在重新psented在我的DataContext对象的对象$ P $。我写了下面code调用这些方法,并显示调用这些方法后产生的T-SQL:

CompositeSIDGroupMap and CompositeSIDMap are both tables in our database that are represented as objects in my DataContext object. I wrote the following code to call these methods and display the T-SQL generated after calling these methods:

using (CodeLookupAccessDataContext codeLookup = new CodeLookupAccessDataContext())
{
    codeLookup.Log = Console.Out;
    List<CompositeSIDMap> compList = codeLookup.lookupCompositeSIDMap(regionId, marketId);
}

我在我的日志下面的结果调用此code后:

I got the following results in my log after invoking this code:

SELECT [t0].[CompositeSIDGroup]
FROM [dbo].[CompositeSIDGroupMap] AS [t0]
WHERE ([t0].[RegionID] = @p0) AND ([t0].[MarketID] = @p1)
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [5]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [3]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1

SELECT [t0].[PK_CSM], [t0].[CompositeSIDGroup], [t0].[InputSID], [t0].[TargetSID], [t0].[StartOffset], [t0].[EndOffset], [t0].[Scale]
FROM [dbo].[CompositeSIDMap] AS [t0]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1

第一个T-SQL语句包含一个WHERE子句指定,并返回一列如预期。但是,第二个语句缺少一个where子句,并返回所有列,即使我没有指定我想查看和哪些行纵队的兴趣。为什么第二个T-SQL语句生成的方式,就是和我应该做的,以确保我根据通过T-SQL?

The first T-SQL statement contains a where clause as specified and returns one column as expected. However, the second statement is missing a where clause and returns all columns, even though I did specify which rows I wanted to view and which columns were of interest. Why is the second T-SQL statement generated the way it is, and what should I do to ensure that I filter out the data according to specifications via the T-SQL?

另外请注意,我将preFER保持查找codeINFO(),尤其是有兴趣在保持它能够接受lambda函数来指定要返回的行/列。

Also note that I would prefer to keep lookupCodeInfo() and especially am interested in keeping it enabled to accept lambda functions for specifying which rows/columns to return.

更新

这种讨论也可能会感兴趣。

推荐答案

现在的问题是,该功能

private List<TResult> lookupCodeInfo<T, TResult>(Func<T, bool> compLambda, 
                                                 Func<T, TResult> selectLambda)

接受 Func键&LT; ...&GT; 参数,这将使编译成函数的lambda表达式。在LINQ到SQL的SQL生成不能转换编译功能,SQL,而只会执行内存中的过滤和投影。

accepts Func<...> arguments, which will make the lambdas compiled into functions. The Linq-to-sql SQL generator cannot translate a compiled function to SQL, but will rather perform the filtering and projection in memory.

更改查找codeINFO 防爆pression&LT; ...&GT; 而不是,这将preserve他们作为EX pression树木LINQ到SQL可以穿越。

Change your lookupCodeInfo to take Expression<...> instead, which will preserve them as expression trees that linq-to-sql can traverse.

 
精彩推荐
图片推荐