2009-03-19 3 views

답변

4

좀 프롬프트 응답을 받았다 않았고, 나는 그것에 대해 모두에게 감사하지만. 내가 가지고있는 답안에 나타난 방법은 효과가 없었습니다.

프레드릭 오엘 렛의 게시물에서 내가 필요한 내용을 결국 Microsoft Forums에 올릴 때까지 계속 검색해야했습니다. 나는 다음과 같은 오류받을이 방법을 사용하면

public static IQueryable<T> WhereIn<T, TValue>(this IQueryable<T> source, Expression<Func<T, TValue>> propertySelector, params TValue[] values) 
    { 
     return source.Where(GetWhereInExpression(propertySelector, values)); 
    } 

    public static IQueryable<T> WhereIn<T, TValue>(this IQueryable<T> source, Expression<Func<T, TValue>> propertySelector, IEnumerable<TValue> values) 
    { 
     return source.Where(GetWhereInExpression(propertySelector, values)); 
    } 

    private static Expression<Func<T, bool>> GetWhereInExpression<T, TValue>(Expression<Func<T, TValue>> propertySelector, IEnumerable<TValue> values) 
    { 
     ParameterExpression p = propertySelector.Parameters.Single(); 
     if (!values.Any()) 
      return e => false; 

     var equals = values.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue)))); 
     var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal)); 

     return Expression.Lambda<Func<T, bool>>(body, p); 
    } 
1

이것은 SQL에있는 쿼리의 정확한 표현입니다.

int [] productList = new int [] {1, 2, 3};

var myProducts = from p in db.Products 
       where productList.Contains(p.ProductID) 
       select p; 

엔티티 인 경우 문제를 더 잘 설명 할 수 있습니까?

정확한 SQL 표현이 될 것이다 ...

SELECT [t0].[ProductID], [t0].[Name], [t0].[ProductNumber], [t0].[MakeFlag], [t0].[FinishedGoodsFlag], 
[t0].[Color], [t0].[SafetyStockLevel], [t0].[ReorderPoint], [t0].[StandardCost], [t0].[ListPrice], 
[t0].[Size], [t0].[SizeUnitMeasureCode], [t0].[WeightUnitMeasureCode], [t0].[Weight], [t0].[DaysToManufacture], 
[t0].[ProductLine], [t0].[Class], [t0].[Style], [t0].[ProductSubcategoryID], [t0].[ProductModelID], 
[t0].[SellStartDate], [t0].[SellEndDate], [t0].[DiscontinuedDate], [t0].[rowguid], [t0].[ModifiedDate] 
FROM [Production].[Product] AS [t0] 
WHERE [t0].[ProductID] IN (@p0, @p1, @p2, @p3) 
3

은 당신이 뭔가를 할 수 있습니다 :

int[] productList = new int[] { 1, 2, 3, 4 }; 

var myProducts = from p in db.Products 
       where productList.Contains(p.ProductID) 
       select p; 
+0

한 빠른을 writting –

+4

에 대한 : 짧은에서

는 아래의 확장 메서드의 엔티티에 LINQ는 방법을 인식하지 못하는 경우를 '부울이 포함 [ Int32] (System.Collections.Generic.IEnumerable'1 [System.Int32], Int32) '메서드를 사용하여이 메서드를 저장소 식 –