How can I filter a DataSource before applying it to a ListView


I have an Entity datasource, which I need to filter before passing it to the ListView. Here is the unfiltered version (works great):

DataContext db = new DataContext(); ListView1.DataSource = db.Cars; ListView1.DataBind();

I'm trying to understand how to select only the Cars that are blue (a field/property/row in the database), and pass just those Cars to the ListView. I've been trying variations on this:

String selectedColor = "blue"; DataContext db = new DataContext(); ListView1.DataSource = db.Cars.Any(m => m.Cars.color == selectedColor); ListView1.DataBind();


Shouldn't this be more like,

ListView1.DataSource = db.Cars.Where(car => car.Color == selectedColor);

Your db.Cars.Any(m => m.Cars.color == selectedColor) [Correction: should be m.Color instead of m.Cars.Color if you wanted to use it by the way] statement will return true if in your Cars collection there's at least one car with color value as that of the selectedColor.


Any() Returns a bool. You need Where()


I would also wrap your context using a using statement. This should take care of your context's disposal automatically.

String selectedColor = "blue"; using(DataContext db = new DataContext()) { ListView1.DataSource = db.Cars.Where(m => m.Cars.color == selectedColor); ListView1.DataBind(); }


  • Listview Large Icon right click to open ContextMenuStrip
  • pass the value from activity to next activity in android
  • Winforms ListView MouseUp event firing more than once
  • c# merge objects
  • JQuery Passing Variable From href to load()
  • How can I hide empty columns in a gridview without knowing which will be empty?
  • ListView selectedindexchanged
  • How to implement cascading DropDownList ASP.Net control? [closed]
  • Individual Preferences for each item in a ListView?
  • Can someone explain the exact use of interfaces in C#?
  • Bind drop down list with Dictionary
  • UIButton Borders Function Only Gives Back White Borders
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Responsive left sidebar open close
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Database structure design with variable amounts of fields
  • Control modification in presentation layer
  • Using jQuery closest() method with class selector
  • Linq Objects Group By & Sum
  • Retrieving value from sql ExecuteScalar()
  • Array.prototype.includes - not transformed with babel
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • ORA-29908: missing primary invocation for ancillary operator
  • jquery mobile loadPage not working
  • How to get next/previous record number?
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • KeystoneJS: Relationships in Admin UI not updating
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Unable to use reactive element in my shiny app