Is there a C# IN operator?

后端 未结 14 1837
说谎
说谎 2020-12-08 03:51

In SQL, you can use the following syntax:

SELECT *
FROM MY_TABLE
WHERE VALUE_1 IN (1, 2, 3)

Is there an equivalent in C#? The IDE seems to

相关标签:
14条回答
  • Common, LINQ way more powerful:

    var list = new List<string> { "Tomato", "Orange", "Mango"};
    var query = from i in my_table
                from v in list
                where i.Name.StartsWith(v)
                select i;
    
    0 讨论(0)
  • 2020-12-08 04:06

    Duplicate of : LINQ to SQL in and not in

    select * from table where fieldname in ('val1', 'val2') 
    

    or

    select * from table where fieldname not in (1, 2) 
    

    The equivalent of IN and NOT IN queries in LINQ to SQL would be something like this:

    List<string> validValues = new List<string>() { "val1", "val2"}; 
    var qry = from item in dataContext.TableName 
              where validValues.Contains(item.FieldName) 
              select item; 
    

    and this:

    List<int> validValues = new List<int>() { 1, 2}; 
    var qry = from item in dataContext.TableName 
              where !validValues.Contains(item.FieldName) 
              select item; 
    
    0 讨论(0)
  • 2020-12-08 04:12

    There is no in operator that looks for a value in a collection, instead it's a method of the collection, called Contains.

    The most scalable solution is to use a HashSet as the collection. Checking for a value in a HashSet is close to an O(1) operation, compared to doing it in a List where it is an O(n) operation. That means that you can pack a lot of values in a HashSet and it's still fast, while looking for a value in a List gets slower the more values you have.

    Example:

    var set = new HashSet<int>();
    set.Add(1);
    set.Add(2);
    set.Add(3);
    
    var result = items.Select(i => set.Contains(i.value));
    
    0 讨论(0)
  • 2020-12-08 04:13

    I agree the best way to implement the In operator is with an Extension Method. I did it a little differently:

    public static bool In(this string str, string CommaDelimintedStringSet)
    {
        string[] Values = CommaDelimintedStringSet.Split(new char[] { ',' });
        foreach (string V in Values)
        {
           if (str == V)
             return true;
        }
        return false;
    }
    

    The difference is that you don't have to put quotes around each value, only the entire set of comma delimited values, which is easier to type:

    bool result = MyString.In("Val1,Val2,Val3");
    
    0 讨论(0)
  • 2020-12-08 04:14

    There's no "in" operator in C#, the "in" keyword is used only with "foreach (... in ...)" or "from ... in ...".

    The LINQ equivalent of your SQL query would be:

    List<int> list = new List<int> { 1, 2, 3 };
    var query = from row in my_table
                where list.Contains(row.value1)
                select row;
    
    0 讨论(0)
  • 2020-12-08 04:15

    I do something like this:

    var shippingAddress = checkoutContext.Addresses.Where(a => (new HashSet<AddressType> { AddressType.SHIPPING_ONLY, AddressType.BILLING_AND_SHIPPING }).Contains(a.AddressType) && a.Id == long.Parse(orderDto.ShippingAddressId)).FirstOrDefault();
    
    0 讨论(0)
提交回复
热议问题