Linq to Entities - SQL “IN” clause

前端 未结 8 1255
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 05:10

In T-SQL you could have a query like:

SELECT * FROM Users WHERE User_Rights IN (\"Admin\", \"User\", \"Limited\")

How would you replicate t

相关标签:
8条回答
  • 2020-11-22 05:48

    You need to turn it on its head in terms of the way you're thinking about it. Instead of doing "in" to find the current item's user rights in a predefined set of applicable user rights, you're asking a predefined set of user rights if it contains the current item's applicable value. This is exactly the same way you would find an item in a regular list in .NET.

    There are two ways of doing this using LINQ, one uses query syntax and the other uses method syntax. Essentially, they are the same and could be used interchangeably depending on your preference:

    Query Syntax:

    var selected = from u in users
                   where new[] { "Admin", "User", "Limited" }.Contains(u.User_Rights)
                   select u
    
    foreach(user u in selected)
    {
        //Do your stuff on each selected user;
    }
    

    Method Syntax:

    var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Contains(u.User_Rights));
    
    foreach(user u in selected)
    {
        //Do stuff on each selected user;
    }
    

    My personal preference in this instance might be method syntax because instead of assigning the variable, I could do the foreach over an anonymous call like this:

    foreach(User u in users.Where(u => new [] { "Admin", "User", "Limited" }.Contains(u.User_Rights)))
    {
        //Do stuff on each selected user;
    }
    

    Syntactically this looks more complex, and you have to understand the concept of lambda expressions or delegates to really figure out what's going on, but as you can see, this condenses the code a fair amount.

    It all comes down to your coding style and preference - all three of my examples do the same thing slightly differently.

    An alternative way doesn't even use LINQ, you can use the same method syntax replacing "where" with "FindAll" and get the same result, which will also work in .NET 2.0:

    foreach(User u in users.FindAll(u => new [] { "Admin", "User", "Limited" }.Contains(u.User_Rights)))
    {
        //Do stuff on each selected user;
    }
    
    0 讨论(0)
  • 2020-11-22 05:48

    Real example:

    var trackList = Model.TrackingHistory.GroupBy(x => x.ShipmentStatusId).Select(x => x.Last()).Reverse();
    List<int> done_step1 = new List<int>() {2,3,4,5,6,7,8,9,10,11,14,18,21,22,23,24,25,26 };
    bool isExists = trackList.Where(x => done_step1.Contains(x.ShipmentStatusId.Value)).FirstOrDefault() != null;
    
    0 讨论(0)
  • 2020-11-22 05:49

    An alternative method to BenAlabaster answer

    First of all, you can rewrite the query like this:

    var matches = from Users in people
            where Users.User_Rights == "Admin" ||
                  Users.User_Rights == "Users" || 
                  Users.User_Rights == "Limited"
            select Users;
    

    Certainly this is more 'wordy' and a pain to write but it works all the same.

    So if we had some utility method that made it easy to create these kind of LINQ expressions we'd be in business.

    with a utility method in place you can write something like this:

    var matches = ctx.People.Where(
            BuildOrExpression<People, string>(
               p => p.User_Rights, names
            )
    );
    

    This builds an expression that has the same effect as:

    var matches = from p in ctx.People
            where names.Contains(p.User_Rights)
            select p;
    

    But which more importantly actually works against .NET 3.5 SP1.

    Here is the plumbing function that makes this possible:

    public static Expression<Func<TElement, bool>> BuildOrExpression<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();
    
        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);
    }
    

    I'm not going to try to explain this method, other than to say it essentially builds a predicate expression for all the values using the valueSelector (i.e. p => p.User_Rights) and ORs those predicates together to create an expression for the complete predicate

    Source: http://blogs.msdn.com/b/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx

    0 讨论(0)
  • 2020-11-22 05:51

    I also tried to work with an SQL-IN-like thing - querying against an Entity Data Model. My approach is a string builder to compose a big OR-expression. That's terribly ugly, but I'm afraid it's the only way to go right now.

    Now well, that looks like this:

    Queue<Guid> productIds = new Queue<Guid>(Products.Select(p => p.Key));
    if(productIds.Count > 0)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("{0}.ProductId = Guid\'{1}\'", entities.Products.Name, productIds.Dequeue());
        while(productIds.Count > 0)
        {
            sb.AppendFormat(" OR {0}.ProductId = Guid\'{1}\'",
              entities.Products.Name, productIds.Dequeue());
        }
    }
    

    Working with GUIDs in this context: As you can see above, there is always the word "GUID" before the GUID ifself in the query string fragments. If you don't add this, ObjectQuery<T>.Where throws the following exception:

    The argument types 'Edm.Guid' and 'Edm.String' are incompatible for this operation., near equals expression, line 6, column 14.

    Found this in MSDN Forums, might be helpful to have in mind.

    Matthias

    ... looking forward for the next version of .NET and Entity Framework, when everything get's better. :)

    0 讨论(0)
  • 2020-11-22 05:52

    This should suffice your purpose. It compares two collections and checks if one collection has the values matching those in the other collection

    fea_Features.Where(s => selectedFeatures.Contains(s.feaId))
    
    0 讨论(0)
  • 2020-11-22 05:55

    I will go for Inner Join in this context. If I would have used contains, it would iterate 6 times despite if the fact that there are just one match.

    var desiredNames = new[] { "Pankaj", "Garg" }; 
    
    var people = new[]  
    {  
        new { FirstName="Pankaj", Surname="Garg" },  
        new { FirstName="Marc", Surname="Gravell" },  
        new { FirstName="Jeff", Surname="Atwood" }  
    }; 
    
    var records = (from p in people join filtered in desiredNames on p.FirstName equals filtered  select p.FirstName).ToList(); 
    

    Disadvantages of Contains

    Suppose I have two list objects.

    List 1      List 2
      1           12
      2            7
      3            8
      4           98
      5            9
      6           10
      7            6
    

    Using Contains, it will search for each List 1 item in List 2 that means iteration will happen 49 times !!!

    0 讨论(0)
提交回复
热议问题