代码之家  ›  专栏  ›  技术社区  ›  Ty.

如何在Linq to Entities 3.5中执行“Where in Values”

  •  20
  • Ty.  · 技术社区  · 16 年前

    有人知道如何使用linq将“Where-in-Values”类型条件应用于实体吗?我试过以下方法,但不起作用:

    var values = new[] { "String1", "String2" };  // some string values
    
    var foo = model.entitySet.Where(e => values.Contains(e.Name));
    

    我相信这在Linq to SQL中有效吗?有什么想法吗?

    7 回复  |  直到 16 年前
        2
  •  19
  •   Paolo    14 年前


    var listOfIds=GetAListOfIds();
    var context=CreateEntityFrameworkObjectContext();
    var results = from item in context.Items
                  where listOfIds.Contains(item.Category.Id)
                  select item;
    //results contains the items with matching category Ids
    

        3
  •  1
  •   DamienG    16 年前

        4
  •  1
  •   Changgyu Oh    15 年前

    var ids = "12, 34, 35";
    using (context = new Entites())
    {
        var selectedProducts = context.CreateQuery<Products>(
            String.Format("select value p from [Entities].Products as p 
                           where p.productId in {{{0}}}", ids)).ToList();
        ...
    }
    
        5
  •  1
  •   Lucian    14 年前

    http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Data.Objects;
    
    namespace Sample {
        public static class Extensions {
            public static IQueryable<T> ExtWhereIn<T, TValue>(this ObjectQuery<T> query,
                        Expression<Func<T, TValue>> valueSelector,
                        IEnumerable<TValue> values) {
                return query.Where(BuildContainsExpression<T, TValue>(valueSelector, values));
            }
            public static IQueryable<T> ExtWhereIn<T, TValue>(this IQueryable<T> query,
                Expression<Func<T, TValue>> valueSelector,
                IEnumerable<TValue> values) {
                return query.Where(BuildContainsExpression<T, TValue>(valueSelector, values));
            }
            private static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(
                    Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values) {
                if (null == valueSelector) { throw new ArgumentNullException("valueSelector"); }
                if (null == values) { throw new ArgumentNullException("values"); }
                ParameterExpression p = valueSelector.Parameters.Single();
                // p => valueSelector(p) == values[0] || valueSelector(p) == ...
                if (!values.Any()) {
                    return e => false;
                }
                var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
                var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
                return Expression.Lambda<Func<TElement, bool>>(body, p);
            }
        }
        class Program {
            static void Main(string[] args) {
                List<int> fullList = new List<int>();
                for (int i = 0; i < 20; i++) {
                    fullList.Add(i);
                }
    
                List<int> filter = new List<int>();
                filter.Add(2);
                filter.Add(5);
                filter.Add(10);
    
                List<int> results = fullList.AsQueryable().ExtWhereIn<int, int>(item => item, filter).ToList();
                foreach (int result in results) {
                    Console.WriteLine(result);
                }
            }
        }       
    }
    

    class Product {
        public int Id { get; set; }
        /// ... other properties
    }
    
    
    List<Product> GetProducts(List<int> productIds) {    
        using (MyEntities context = new MyEntities()) {
            return context.Products.ExtWhereIn<Product, int>(product => product.Id, productIds).ToList();
        }
    }
    
        6
  •  0
  •   Aaron Powell    16 年前

        7
  •  0
  •   Gabe    16 年前

    var results = from p in db.Products
    
                 where p.Name == nameTextBox.Text
    
                 select p;