T-SQL generated from LINQ to SQL does not contain where clause

I have a DataContext called "CodeLookupAccessDataContext" that was generated using the Visual Studio LINQ to SQL Class Wizard. I have extended the functionality of this object so that it provides some methods for returning the 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 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);
}

      

I got the following results in my log after calling 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

      

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

Also note that I would prefer to keep lookupCodeInfo () and is particularly interested in allowing it to accept lambda functions to specify the rows / columns to return.

UPDATE

This discussion may also be of interest.

+2


a source to share


1 answer


The problem is that the function

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

      



takes arguments Func<...>

to be compiled by lambdas in the function. The Linq-to-sql SQL generator cannot translate a compiled function into SQL, but rather does filtering and in-memory projection.

Modify lookupCodeInfo

instead Expression<...>

to store them as expression trees that linq-to-sql can traverse.

+5


a source







All Articles