score:12
Accepted answer
Sure. You can write a method like:
public Expression<Func<Customer, bool>> GetDave()
{
return c => c.FirstName == "Dave"
&& c.IsActive
&& c.HasAddress;
}
...and repository methods like:
public IEnumerable<Customer> GetOneGuy(Expression<Func<Customer, bool>> criteria)
{
return Context.Customers.Where(criteria);
}
...and call:
var dave = Repository.GetOneGuy(this.GetDave()).Single();
score:0
You can build an expression and use it later
var isg = Guid.TryParse(id, out gLT);
Expression<Func<YourObjectResultType, bool>> query = w => w.Id == id;
if (isg) {
query = w => w.Token == gLT;
}
var result = _context.TblYourTable.Where(query);
score:1
If you just want to capture these kinds of queries in a re-usable way, you might implement this as extension methods on IQueryable<Customer>
(or whatever POCO you want). Something like:
public static class ExtnensionsForIQueryableCustomer
{
public static IEnumerable<Customer> WhereActiveWithAddressAndNamed (this IQueryable<Customer> queryable, string name)
{
return queryable.Where (c => c.FirstName == name)
.Where (c => c.IsActive)
.Where (c => c.HasAddress);
}
}
You could then consume this like:
customerRepository.GetAll ().WhereActiveWithAddressAndNamed ("Dave");
Source: stackoverflow.com
Related Query
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Is there any way to use Linq to SQL without adding the .dbml file?
- c# linq generated query length over the limit. Is there any way we could up lift this limit?
- Is there a way to run the query and inspect the aggregate data without rerunning the query again?
- Is there any way to pass the lambda expression as a variable or argument?
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- Is there an easy way to append lambdas and reuse the lambda name in order to create my Linq where condition?
- Create linq query to search for contact the same way smartphone will do so
- Is there a way to create an Linq XElement iterator that is updated as the tree is modified?
- Is there a way to add or remove a line of an LINQ to DB Query based on if the value being checked is null?
- Is there a way to speed up the contains extension in a LINQ query for large datasets?
- Is there any way to encapsulate a simple LINQ query in an extension method that can be used with LINQ to Entities query?
- is there any linqish way to save user form data - Re factoring my code
- Create LINQ intermediate variable the way .Select it does
- How to create a Linq query to check if data exists in the database?
- Better way of assigning the variable during LINQ Query
- How do I create a linq query where the select column name is a variable
- Can I assign the result of a Linq query to the source variable of the same query?
- Is there a way to use Linq to display an item with the highest result without duplication of a specific name?
- Is there any way in LINQ to pass value from the selected object
- Is there any better way to check if the same data is present in a table in .Net core 3.1?
- How to assign LINQ Query to a variable and then use it later in the code
- Is there any way to get the update date in linq and c#?
- How can I convert my LINQ query to use the lambda syntax and is there any advantage to doing this?
- Is there a way to use linq query with DateTime and a string working together in the same request?
- Is there any way I can Optimise my linq query to reduce database calls?
- Data paging in linq without removing records from the data source
- better way to do a linq query - need to reach foreign keyed data by traversing the data structure
- Is there any way to "name" or "tag" a LINQ query so that it can be identified by a tool like Ignite SQL easier?
- Linq query not returning any data to the Datagridview (Possible failure on linq query) C#
More Query from same tag
- Entity SQLCE Can't find connection string in app.config file
- how to remove a character at the end of a string in linq c#
- How to check null reference before it throws an exception?
- Convert LINQ query to SQL
- Query Data Using LINQ C# With Filter string[] array?
- How to group data from table with multiple columns
- Entity Framework4, to see generated query?
- Conditional Join with LINQ
- left outer join in linq error at defaultifempty
- Inheritance in SQL Server and LINQ?
- How to loop though XElements and compare values using C#
- Linq to NHibernate ordering is not working as expected
- Unrecognised method call with NHibernate and Linq when only date part required
- how to improve LInQ performance repository pattern
- Loading xml data to ListView in C#?
- I have two large lists and I need get the diff between them
- Possible to do two Group Bys and an if count check in one LINQ lambda statement?
- C# Linq union multiple properties to one list
- What are the benefits of a Deferred Execution in LINQ?
- A linq statement with multiple group by columns not allowing a thenby
- Linq data mapping: usage of Storage property on column attribute
- What does this C# code with an "arrow" mean and how is it called?
- Problem getting XML elements in an SVG file
- Find inconsistent and consecutive objects from List in C#
- Linq sql - where should I put phrase with "where"
- How to check for null on building expression using System.Linq.Dynamic.Core
- Ordering list by many conditions using Linq
- Pass in Array to Select Case
- How to replace lambda written in Where clause of Linq with equivalent delegate
- LINQ, GroupBy inner value of field which has type of object