none
Multiple Select over a List RRS feed

  • Question

  • Suppose I have a List<string> and I want to select based on the multiple strings in that list

    For one item I could do

    var query = from p in ctx.Table where p.Id == 1 select p;

    but I would like to iterate through the List<string> for my Where clause so that

    foreach(string Id in listOfString)

    {

         p.Id = Id

    }

    Any ideas? Thanks

    Thursday, June 10, 2010 2:50 PM

Answers

  • Perhaps I'm misunderstanding, you want to select all records where p.Id==whatever is in that list

     Then use below code

    var query= from p in ctx.Table where listOfString.Contains(p.Id) select p;

    • Edited by Serguey123 Thursday, June 10, 2010 3:30 PM
    • Proposed as answer by John T. Angle Thursday, June 10, 2010 5:49 PM
    • Marked as answer by k2byte Thursday, June 10, 2010 6:08 PM
    Thursday, June 10, 2010 3:10 PM

All replies

  • Perhaps I'm misunderstanding, you want to select all records where p.Id==whatever is in that list

     Then use below code

    var query= from p in ctx.Table where listOfString.Contains(p.Id) select p;

    • Edited by Serguey123 Thursday, June 10, 2010 3:30 PM
    • Proposed as answer by John T. Angle Thursday, June 10, 2010 5:49 PM
    • Marked as answer by k2byte Thursday, June 10, 2010 6:08 PM
    Thursday, June 10, 2010 3:10 PM
  • That's correct.
    Thursday, June 10, 2010 3:25 PM
  • That worked... thank you.

    Thursday, June 10, 2010 6:08 PM
  •  

     

    I have a very similar Problem:

     

     

    var idlist = (from v in pMapList select v.Id).ToList();
    var b = idlist.Contains(1); //true
    b = idlist.Contains(2);  //true
    b = idlist.Contains(4);  //false
    b = idlist.Contains(999); //true
    
    var lQuery = from p in EntityContext.SomeKindOfTbl where idlist.Contains(p.map_id) select p;

    This Works:

     

     

    var ret = new List<MyTblType>();
    
    IQueryable<MyTblType> lQuery;
    
    foreach (var pMap in pAccessMapList)
    {
     lQuery = from r in EntityContext.MyTbl
            where pMap.Id == r.map_id
            select r;
    
      if (lQuery.Count() > 0)
       ret.AddRange(lQuery);
    }

     

     

    Any Ideas?

    THX!

    Friday, June 11, 2010 2:53 PM
  • Similar but not the same, there are severals ways to handle this but if you want the answer to be the same then override the way this object pMap type handles equality then yes, your code will be

    var ret from r in EntityContext.MyTbl where pAccessMapList.Contains(r) select r;

    Friday, June 11, 2010 3:25 PM
  • A Frien Shows me a nice link:

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

     

    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);
    
    }

    It works fine:
    var lQuery = EntityContext.MyTbl.Where(BuildContainsExpression<MyTbl, int>(e => e.map_id, (from v in pMapList select v.Id).ToList())).Where(v => v.lm_id == pModeId && v.lms_id == pModeStateId);

    Is there any Bad Performance influence to use a Whereon the End of the Whare Clause?

    When Yes, is there any Guy who coult extense the generic funktion aboth to make the secon where clause obsolete?
    Ich have tried it but its some levels to high for me

    Friday, June 11, 2010 5:06 PM
  • What do you want to know? What answer is best, simpler, doable?

    There are constraints to answers always, I always prefer to hide as much complexity as possible to the person using something.

    So what I sugested is to either create a custom comparer

    http://msdn.microsoft.com/en-us/library/bb339118.aspx

    or override how your object is equal to other

    http://msdn.microsoft.com/en-us/library/bb352880.aspx

    Friday, June 11, 2010 5:29 PM
  • Sorry, but i thing it wont work your way. its not a list of objects. ists a list<int>.
    A Contain on a List of int32 with an int32 should work native, but it doesn't...

     

    Exceptiontext:  "'LINQ to Entities' erkennt die Methode 'Boolean Contains(Int32)' nicht, und diese Methode kann nicht in einen Speicherausdruck übersetzt werden"

    Monday, June 14, 2010 9:19 AM
  • I'm not german so runned the exception message by google, sorry if I misunderstood.

    Now linq to entities is a touchy one, the reason are plenty, would you post the entire code you want to convert with comment so I can better tell you.

    Monday, June 14, 2010 12:42 PM