score:2
Accepted answer
the answer to this question is a bit more complicated than it looks. in order to let linq execute c# code you need to make the function an expression so the in and output will be interperted not as code but as some sort of a meaning. the solution looks like this:
private expression<func<tpeople, bool>> getdefaultdomainaccount<tpeople>(func<domainaccount, bool> f) where tpeople : person
{
return (a) => f(a.domainaccounts.firstordefault(d => d.domainaccounttype == domainaccounttype.standarduser));
}
now the code can be called uppon like this:
public iqueryable<tpeople> getpeoplebyusername<tpeople>(string username) where tpeople : person
{
getpeople<tpeople>().where(getdefaultdomainaccount<tpeople>(d => d.username == username));
return people;
}
instead of this:
public iqueryable<tpeople> getpeoplebyusername<tpeople>(string username) where tpeople : person
{
username = username.toupper();
var people = getpeople<tpeople>()
.where(a => a.domainaccounts.firstordefault(d => d.domainaccounttype == domainaccounttype.standarduser).username.toupper().contains(username));
return people;
}
Source: stackoverflow.com
Related Query
- Reuse Linq to SQL code with entityframework
- Linq to sql as object data source - designer problem with partial classes
- Accessing SQL Server time in code with LINQ
- SQL Unions with table counts using EntityFramework LINQ query
- Converting SQL code with Row_Number() into LINQ C# code
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- Reuse stored procedure result in linq to sql in Code First
- LINQ to SQL - Left Outer Join with multiple join conditions
- Instantiate empty IQueryable for use with Linq to sql
- Convert SQL to Linq left join with null
- How are people unit testing code that uses Linq to SQL
- Linq To Sql with PostgreSQL
- How do I most elegantly express left join with aggregate SQL as LINQ query
- Paginated search results with LINQ to SQL
- Most efficient way to update with LINQ to SQL
- Odd behavior in LINQ to SQL with anonymous objects and constant columns
- Translate SQL to lambda LINQ with GroupBy and Average
- get last element with linq to sql
- Why didn't the LINQ designers stick with using the way sql is written today?
- Returning a single value with Linq to SQL
- Can I use C# string interpolation with Linq to SQL
- linq to sql query with multiple where parameters
- How to implement SkipWhile with Linq to Sql without first loading the whole list into memory?
- How to use distinct with group by in Linq to SQL
- Make Linq to Sql generate T-SQL with ISNULL instead of COALESCE
- Deleting rows in a table with Linq to SQL
- Simple sql to Linq query with group by and aggregate functions
- How To Create Generic Data Access Object (DAO) CRUD Methods with LINQ to SQL
- LINQ to SQL entity column name attribute ignored with guid primary key
- Separating concerns with Linq To SQL and DTO's
More Query from same tag
- Cannot implicitly convert IQueryable
- Selecting a multi-dimensional array in LINQ
- LINQ provider to a Web API causing lots of grief
- OrderByDescending in lambdaExpression is not accepting parameters
- DENSE_RANK() using LINQ without intermediary steps
- LINQ Grouping by ParentId
- Using SQL "CAST" within LINQ
- Independent subquery in linq
- Linq, how to delete items in the middle of a query return?
- MVC3. How to select additional column from joined table and send them all to View?
- How to convert SQL to LINQ with look-ahead (lead like query)
- Simple Sql query conversion to Linq To Sql
- Quicksort with Linq performance Advantage passing T[] vs. IEnumerable<T>
- Compare and check collections using LINQ
- Get a list from a dynamic json result
- Split field a string and compare each value with string linq c#
- SELECT Unique rows from Datagridview using LINQ
- Linq method based left join with multiple criteria
- How do I get values from SelectedItem in ComboBox with Linq and C# 3.5
- Linq List to share all except duplicate with certain status
- Tunning my dash board linq query
- I've just upgraded from a VS2010 project, but my linq query now won't sum an anonymous type
- Expression Tree as part of a Property
- Need help in properly writing an XML LINQ query
- Search for overlapping date ranges
- Fastest way to find index of differences in two arrays
- How do I build up LINQ dynamically
- Filter data from MongoDB with .NET driver
- Query data using "Contains" keyword in Dynamic Linq in C#
- Linq Expression for Paging / Pagination with dynamic OrderBy