score:1
Accepted answer
i think what you are looking for is:
var zippredicate = predicatebuilder.false<nameaddress>();
list<string> zips = new list<string>();
zips.add("90210");
zips.add("90211");
foreach (var item in zips)
{
zippredicate = zippredicate.or(n=> n.zip.contains(item) && n.purpose=="street address");
}
var zipresult = from s in nameaddresses
.asexpandable()
.where(zippredicate)
select new{s.id, s.zip, s.purpose};
zipresult.dump();
edit
one more thing as well, if you want to drop building up the predicate, you should be able to do something like: .where(n=>zips.contains(n.zip) && n.purpose=="street address")
the important piece is that your entity property component comes inside the .contains(). this would shorten your code to:
list<string> zips = new list<string>();
zips.add("90210");
zips.add("90211");
var zipresult = from s in nameaddresses
.asexpandable()
.where(n=>zips.contains(n.zip) && n.purpose=="street address")
select new{s.id, s.zip, s.purpose};
zipresult.dump();
which i like better for readability. i would expect the query that ends up getting executed is the same either way.
Source: stackoverflow.com
Related Query
- How to create a predicate that must meet two values with one being a list of values?
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How to convert list of objects with two fields to array with one of them using LINQ
- How to merge two lists that share 1 attribute into a third one with LINQ?
- How to merge two lists of different types into one list of new type that contains both types?
- How can I select using LINQ for an entry that contains a LIST with more than one row?
- Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?
- How to Zip two Lists of different size to create a new list that is same as the size of the longest amongst the original lists?
- How do I compare two lists where one list starts with the other, and only return the "overhang" elements?
- How to use linq to assign two lists and an int to create one list of a new object?
- How to create a new list from property values when grouping items with GroupBy?
- LINQ : Select one item from list but it must match two values
- How do I copy one list to another that has an object with additional properties in C# ? (without a foreach)
- How can I create a list of rows that have one of a list of properties
- How to properly create a union from two tables selecting only specifc columns where one column is going to empty with some text
- How to write SQL translateable linq code that groups by one property and returns distinct list
- Create a list from two object lists with linq
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- How to select values in list that are NOT IN a Table using EF Core?
- Using LINQ. With two different lists. How can I identify objects that do not match
- Merge two Lists in C# and merge objects with the same id into one list item
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- How can I use linq to return integers in one array that do not match up with an integer property of another array?
- How to create a comma delimited string from distinct list of values using LINQ?
- How to create a RavenDB index that returns a list of strings?
- How can I combine this code into one or two LINQ queries?
- How to create a list from filtering 2 lists with linq to object
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- Compare id's of objects in two lists and get the list of objects that includes objects with ids occurring in both of them
- linq - how do you do a query for items in one query source that are not in another one?
More Query from same tag
- Linq. Select from multiple tables
- SQL datetime and calendar.selectedDate in asp format issue
- Best/fastest way to select items from generic list that connect with offset mins
- Does LINQ to SQL always use Transactions by default?
- How to abstract a field in the Select list of a Linq query?
- Select all from one List, replace values that exist on another List
- Refactor LINQ method to introduce Lamba function as a parameter
- Retrieve all item in listbox and select only those which is in db
- How to Sortby Multiple Colums - Linq to SQL
- Join navigation property with line
- Dynamic lambda expression (OrderBy) and nullable property type
- Pivot data using LINQ
- Inner join to get those not in table
- Setting anonymous type property name
- Improving EF Query Performance - checking if rows exist in a prefetched List<string>
- Linq using Aggregate() for List
- LINQ to remove duplicated property
- Linq2SQL get value of dynamically chosen column
- How can I compose a LINQ query with the help of a predicate builder
- LINQ or SQL to get records closest to specific time per day per account
- C# LINQ get records from datatable group by hour
- LINQ to SQL - Update to increment a non-primary-key field - thread-safe
- Acces data from objects in a list
- Optimise and speed up very slow Linq / SQL code
- IQueryable conditional include
- Resolving service in parameterless constructor causes InvalidOperationException in Repository
- Fetching data from more than 2 tables using EntityFramework
- LINQ Short constructor as action
- How to group a table by date and company id in C# and Entity Framework
- Advanced LINQ filtering