0

Edit: Code works fine, it was an other bug.

I had comment out the //department.IdAgency = reader.GetByte(2); line, in the created departmentList. When I removed the // then the IQueryable<string> with .Where works fine. Sorry for the inconvenience!

static List<Department> CreateDepartmentList(IDataReader reader)
{
  List<Department> departmentList = new List<Department>();
  Department department = null;
  while (reader.Read())
  {
    department = new Department();
    department.Id = reader.GetByte(0);
    department.Name = reader.GetString(1);
    //department.IdAgency = reader.GetByte(2);

    if (!reader.IsDBNull(3))
    { department.IdTalkGroup = reader.GetInt16(3); }
    departmentList.Add(department);
  }
  return departmentList;
}

Original question:

I have an IQueryable<string> query, that works. But how do I use .Where?

IQueryable<string> query = departmentList.AsQueryable()
                                         .OrderBy(x => x.Name)
                                         .Select(x => x.Name);

I have tried this, but it does not work:

IQueryable<string> query = departmentList.AsQueryable()
                                         .OrderBy(x => x.Name)
                                         .Where(x => x.IdAgency == idAgencySelected[0])
                                         .Select(x => x.Name);
6
  • usually, a where clause comes before an ordering. But what do you mean by "does not work" ? Commented Apr 10, 2014 at 19:25
  • I have tried to insert Where before OrderBy, with the same result. I get an empty list, when I use Where. I have check that x.IdAgency contain the right number.
    – MHP
    Commented Apr 10, 2014 at 19:27
  • This still doesn't answer "does not work". Commented Apr 10, 2014 at 19:28
  • With the "does not work" I mean. The result in my DropDownList is empty. When I not use Where, the DropDownList contain all content from a database table. I hope you understand me know.
    – MHP
    Commented Apr 10, 2014 at 19:37
  • Yes, so write this in your question instead of does not work, it will help people to help you ;) Now, what's idAgencySelected, and what do you have, while debugging, in idAgencySelected[0] ? You have something in your datas with that value ? Commented Apr 10, 2014 at 19:39

1 Answer 1

1

All the .Where() call does is apply a filtering method to each element on the list, thus returning a new IEnumerable.

So, for some IQueryable<string>...

IEnumerable<string> results = SomeStringList.Where(s => s.Contains("Department"));

...You would get a list of strings that contain the word department.

In other words, by passing it some boolean condition that can be applied to a member of the queryable collection, you get a subset of the original collection.

The reason your second block of code does not work, is because you're calling a method or property that does not belong to string. You may want to consider querying against the more complex type, if it has identifier data, and then take the names of the elements and add them to some list instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.