Creating dynamic queries with entity framework
up vote
35
down vote
favorite
I would like to know what is the best way of creating dynamic queries with entity framework and linq.
I want to create a service that has many parameters for sorting and filtering (over 50). I will be getting object from gui where these will be filled out... and query will be executed from a single service method.
I looked around And I saw that I could dynamically create a string that can be executed at the end of my method. I don't like this way very much. Is there a better way to do this? Preferably type safe with compile check?
c# linq entity-framework entity-framework-4
add a comment |
up vote
35
down vote
favorite
I would like to know what is the best way of creating dynamic queries with entity framework and linq.
I want to create a service that has many parameters for sorting and filtering (over 50). I will be getting object from gui where these will be filled out... and query will be executed from a single service method.
I looked around And I saw that I could dynamically create a string that can be executed at the end of my method. I don't like this way very much. Is there a better way to do this? Preferably type safe with compile check?
c# linq entity-framework entity-framework-4
add a comment |
up vote
35
down vote
favorite
up vote
35
down vote
favorite
I would like to know what is the best way of creating dynamic queries with entity framework and linq.
I want to create a service that has many parameters for sorting and filtering (over 50). I will be getting object from gui where these will be filled out... and query will be executed from a single service method.
I looked around And I saw that I could dynamically create a string that can be executed at the end of my method. I don't like this way very much. Is there a better way to do this? Preferably type safe with compile check?
c# linq entity-framework entity-framework-4
I would like to know what is the best way of creating dynamic queries with entity framework and linq.
I want to create a service that has many parameters for sorting and filtering (over 50). I will be getting object from gui where these will be filled out... and query will be executed from a single service method.
I looked around And I saw that I could dynamically create a string that can be executed at the end of my method. I don't like this way very much. Is there a better way to do this? Preferably type safe with compile check?
c# linq entity-framework entity-framework-4
c# linq entity-framework entity-framework-4
edited Apr 4 '11 at 16:24
Gabe Moothart
22.6k126794
22.6k126794
asked Apr 4 '11 at 16:10
Eduard
2,40711627
2,40711627
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
52
down vote
accepted
You could compose an IQueryable<T>
step by step. Assuming you have a FilterDefinition
class which describes how the user wants to filter ...
public class FilterDefinition
{
public bool FilterByName { get; set; }
public string NameFrom { get; set; }
public string NameTo { get; set; }
public bool FilterByQuantity { get; set; }
public double QuantityFrom { get; set; }
public double QuantityTo { get; set; }
}
... then you could build a query like so:
public IQueryable<SomeEntity> GetQuery(FilterDefinition filter)
{
IQueryable<SomeEntity> query = context.Set<SomeEntity>();
// assuming that you return all records when nothing is specified in the filter
if (filter.FilterByName)
query = query.Where(t =>
t.Name >= filter.NameFrom && t.Name <= filter.NameTo);
if (filter.FilterByQuantity)
query = query.Where(t =>
t.Quantity >= filter.QuantityFrom && t.Quantity <= filter.QuantityTo);
return query;
}
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
7
@t-edd: No, it leveragesdeferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means thatIQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator toIQueryable<T>
, for instancequery.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.
– Slauma
Apr 5 '11 at 10:03
It's not that good because it assumes thatSomeEntity
has Name and Quantity fields so this is only half dynamic.
– Maciej Szpakowski
Jan 23 '17 at 17:56
Is there any way to defineand
oror
dynamically?
– Yusril Maulidan Raji
Apr 20 '17 at 8:14
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
|
show 1 more comment
up vote
31
down vote
The only other way that I know of would be to build an IQueryable based on your filter vaues.
public List<Contact> Get(FilterValues filter)
{
using (var context = new AdventureWorksEntities())
{
IQueryable<Contact> query = context.Contacts.Where(c => c.ModifiedDate > DateTime.Now);
if (!string.IsNullOrEmpty(filter.FirstName))
{
query = query.Where(c => c.FirstName == filter.FirstName);
}
if (!string.IsNullOrEmpty(filter.LastName))
{
query = query.Where(c => c.LastName == filter.LastName);
}
return query.ToList();
}
}
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
1
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
add a comment |
up vote
6
down vote
I have created a generic repository which should help you. It supports uniform API to query and sort on both known and dynamic fields:
//Filter on known fields
var keyboard = Query<Product>.Create(p=>p.Category=="Keyboard");
var keyboards = repository.Get(keyboard);
//Or filter on dynamic fields
var filter = Query<Product>.Create("Rating", OperationType.GreaterThan, 4)
var filteredKeyboards = repository.Get(filter);
//You can also combine two queries togather
var filterdKeyboards2 = repository.Get(keyboard.And(filter))
//Order it on known fields
var orderedKeyboard = keyboard.OrderBy(o=>o.Asc(p=>p.Name));
var orderedKeyboards = repository.Get(orderedKeyboard);
//Or order by on dynamic fields
var userOrdering = keyboard.OrderBy(o=>o.Asc("Name"));
var orderedKeyboards2 = repository.Get(userOrdering);
I do not know about the search object/DTO you're getting but you can easily create a generic search object/DTO and can map it to a Query object in few lines of code. I have used it in past around a WCF service and it has worked very well for me.
add a comment |
up vote
1
down vote
You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
52
down vote
accepted
You could compose an IQueryable<T>
step by step. Assuming you have a FilterDefinition
class which describes how the user wants to filter ...
public class FilterDefinition
{
public bool FilterByName { get; set; }
public string NameFrom { get; set; }
public string NameTo { get; set; }
public bool FilterByQuantity { get; set; }
public double QuantityFrom { get; set; }
public double QuantityTo { get; set; }
}
... then you could build a query like so:
public IQueryable<SomeEntity> GetQuery(FilterDefinition filter)
{
IQueryable<SomeEntity> query = context.Set<SomeEntity>();
// assuming that you return all records when nothing is specified in the filter
if (filter.FilterByName)
query = query.Where(t =>
t.Name >= filter.NameFrom && t.Name <= filter.NameTo);
if (filter.FilterByQuantity)
query = query.Where(t =>
t.Quantity >= filter.QuantityFrom && t.Quantity <= filter.QuantityTo);
return query;
}
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
7
@t-edd: No, it leveragesdeferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means thatIQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator toIQueryable<T>
, for instancequery.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.
– Slauma
Apr 5 '11 at 10:03
It's not that good because it assumes thatSomeEntity
has Name and Quantity fields so this is only half dynamic.
– Maciej Szpakowski
Jan 23 '17 at 17:56
Is there any way to defineand
oror
dynamically?
– Yusril Maulidan Raji
Apr 20 '17 at 8:14
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
|
show 1 more comment
up vote
52
down vote
accepted
You could compose an IQueryable<T>
step by step. Assuming you have a FilterDefinition
class which describes how the user wants to filter ...
public class FilterDefinition
{
public bool FilterByName { get; set; }
public string NameFrom { get; set; }
public string NameTo { get; set; }
public bool FilterByQuantity { get; set; }
public double QuantityFrom { get; set; }
public double QuantityTo { get; set; }
}
... then you could build a query like so:
public IQueryable<SomeEntity> GetQuery(FilterDefinition filter)
{
IQueryable<SomeEntity> query = context.Set<SomeEntity>();
// assuming that you return all records when nothing is specified in the filter
if (filter.FilterByName)
query = query.Where(t =>
t.Name >= filter.NameFrom && t.Name <= filter.NameTo);
if (filter.FilterByQuantity)
query = query.Where(t =>
t.Quantity >= filter.QuantityFrom && t.Quantity <= filter.QuantityTo);
return query;
}
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
7
@t-edd: No, it leveragesdeferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means thatIQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator toIQueryable<T>
, for instancequery.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.
– Slauma
Apr 5 '11 at 10:03
It's not that good because it assumes thatSomeEntity
has Name and Quantity fields so this is only half dynamic.
– Maciej Szpakowski
Jan 23 '17 at 17:56
Is there any way to defineand
oror
dynamically?
– Yusril Maulidan Raji
Apr 20 '17 at 8:14
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
|
show 1 more comment
up vote
52
down vote
accepted
up vote
52
down vote
accepted
You could compose an IQueryable<T>
step by step. Assuming you have a FilterDefinition
class which describes how the user wants to filter ...
public class FilterDefinition
{
public bool FilterByName { get; set; }
public string NameFrom { get; set; }
public string NameTo { get; set; }
public bool FilterByQuantity { get; set; }
public double QuantityFrom { get; set; }
public double QuantityTo { get; set; }
}
... then you could build a query like so:
public IQueryable<SomeEntity> GetQuery(FilterDefinition filter)
{
IQueryable<SomeEntity> query = context.Set<SomeEntity>();
// assuming that you return all records when nothing is specified in the filter
if (filter.FilterByName)
query = query.Where(t =>
t.Name >= filter.NameFrom && t.Name <= filter.NameTo);
if (filter.FilterByQuantity)
query = query.Where(t =>
t.Quantity >= filter.QuantityFrom && t.Quantity <= filter.QuantityTo);
return query;
}
You could compose an IQueryable<T>
step by step. Assuming you have a FilterDefinition
class which describes how the user wants to filter ...
public class FilterDefinition
{
public bool FilterByName { get; set; }
public string NameFrom { get; set; }
public string NameTo { get; set; }
public bool FilterByQuantity { get; set; }
public double QuantityFrom { get; set; }
public double QuantityTo { get; set; }
}
... then you could build a query like so:
public IQueryable<SomeEntity> GetQuery(FilterDefinition filter)
{
IQueryable<SomeEntity> query = context.Set<SomeEntity>();
// assuming that you return all records when nothing is specified in the filter
if (filter.FilterByName)
query = query.Where(t =>
t.Name >= filter.NameFrom && t.Name <= filter.NameTo);
if (filter.FilterByQuantity)
query = query.Where(t =>
t.Quantity >= filter.QuantityFrom && t.Quantity <= filter.QuantityTo);
return query;
}
edited Apr 4 '11 at 16:41
answered Apr 4 '11 at 16:33
Slauma
146k51355383
146k51355383
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
7
@t-edd: No, it leveragesdeferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means thatIQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator toIQueryable<T>
, for instancequery.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.
– Slauma
Apr 5 '11 at 10:03
It's not that good because it assumes thatSomeEntity
has Name and Quantity fields so this is only half dynamic.
– Maciej Szpakowski
Jan 23 '17 at 17:56
Is there any way to defineand
oror
dynamically?
– Yusril Maulidan Raji
Apr 20 '17 at 8:14
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
|
show 1 more comment
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
7
@t-edd: No, it leveragesdeferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means thatIQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator toIQueryable<T>
, for instancequery.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.
– Slauma
Apr 5 '11 at 10:03
It's not that good because it assumes thatSomeEntity
has Name and Quantity fields so this is only half dynamic.
– Maciej Szpakowski
Jan 23 '17 at 17:56
Is there any way to defineand
oror
dynamically?
– Yusril Maulidan Raji
Apr 20 '17 at 8:14
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
Thank you, but how dows this work? Doesnt this pull all the data from database and then step by step narrow it down to desired set of data??
– Eduard
Apr 5 '11 at 6:36
7
7
@t-edd: No, it leverages
deferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means that IQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator to IQueryable<T>
, for instance query.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.– Slauma
Apr 5 '11 at 10:03
@t-edd: No, it leverages
deferred execution
(blogs.msdn.com/b/charlie/archive/2007/12/09/…). That means that IQueryable<T>
which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all. You execute the query then by applying a "greedy" operator to IQueryable<T>
, for instance query.ToList()
. At this point - and not earlier - the query expression is translated into SQL and sent to the server.– Slauma
Apr 5 '11 at 10:03
It's not that good because it assumes that
SomeEntity
has Name and Quantity fields so this is only half dynamic.– Maciej Szpakowski
Jan 23 '17 at 17:56
It's not that good because it assumes that
SomeEntity
has Name and Quantity fields so this is only half dynamic.– Maciej Szpakowski
Jan 23 '17 at 17:56
Is there any way to define
and
or or
dynamically?– Yusril Maulidan Raji
Apr 20 '17 at 8:14
Is there any way to define
and
or or
dynamically?– Yusril Maulidan Raji
Apr 20 '17 at 8:14
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
it also doesn't allow for dynamic conditional operators, for example if you wanted to dynamically allow users to filter by "cost > 10" or "cost < 10"
– ferr
Sep 14 '17 at 18:01
|
show 1 more comment
up vote
31
down vote
The only other way that I know of would be to build an IQueryable based on your filter vaues.
public List<Contact> Get(FilterValues filter)
{
using (var context = new AdventureWorksEntities())
{
IQueryable<Contact> query = context.Contacts.Where(c => c.ModifiedDate > DateTime.Now);
if (!string.IsNullOrEmpty(filter.FirstName))
{
query = query.Where(c => c.FirstName == filter.FirstName);
}
if (!string.IsNullOrEmpty(filter.LastName))
{
query = query.Where(c => c.LastName == filter.LastName);
}
return query.ToList();
}
}
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
1
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
add a comment |
up vote
31
down vote
The only other way that I know of would be to build an IQueryable based on your filter vaues.
public List<Contact> Get(FilterValues filter)
{
using (var context = new AdventureWorksEntities())
{
IQueryable<Contact> query = context.Contacts.Where(c => c.ModifiedDate > DateTime.Now);
if (!string.IsNullOrEmpty(filter.FirstName))
{
query = query.Where(c => c.FirstName == filter.FirstName);
}
if (!string.IsNullOrEmpty(filter.LastName))
{
query = query.Where(c => c.LastName == filter.LastName);
}
return query.ToList();
}
}
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
1
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
add a comment |
up vote
31
down vote
up vote
31
down vote
The only other way that I know of would be to build an IQueryable based on your filter vaues.
public List<Contact> Get(FilterValues filter)
{
using (var context = new AdventureWorksEntities())
{
IQueryable<Contact> query = context.Contacts.Where(c => c.ModifiedDate > DateTime.Now);
if (!string.IsNullOrEmpty(filter.FirstName))
{
query = query.Where(c => c.FirstName == filter.FirstName);
}
if (!string.IsNullOrEmpty(filter.LastName))
{
query = query.Where(c => c.LastName == filter.LastName);
}
return query.ToList();
}
}
The only other way that I know of would be to build an IQueryable based on your filter vaues.
public List<Contact> Get(FilterValues filter)
{
using (var context = new AdventureWorksEntities())
{
IQueryable<Contact> query = context.Contacts.Where(c => c.ModifiedDate > DateTime.Now);
if (!string.IsNullOrEmpty(filter.FirstName))
{
query = query.Where(c => c.FirstName == filter.FirstName);
}
if (!string.IsNullOrEmpty(filter.LastName))
{
query = query.Where(c => c.LastName == filter.LastName);
}
return query.ToList();
}
}
answered Apr 4 '11 at 16:33
BrandonZeider
6,91821619
6,91821619
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
1
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
add a comment |
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
1
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
Yes, but is this efective performance wise? When is the select executed? In the end when ToList() is called? Imagine I have very large set of data....
– Eduard
Apr 5 '11 at 6:38
1
1
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
No, it's not a performance hit, as it uses deferred execution to only query once.
– BrandonZeider
Apr 5 '11 at 12:54
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
+1 Thank you for good answer.
– Eduard
Apr 5 '11 at 14:05
add a comment |
up vote
6
down vote
I have created a generic repository which should help you. It supports uniform API to query and sort on both known and dynamic fields:
//Filter on known fields
var keyboard = Query<Product>.Create(p=>p.Category=="Keyboard");
var keyboards = repository.Get(keyboard);
//Or filter on dynamic fields
var filter = Query<Product>.Create("Rating", OperationType.GreaterThan, 4)
var filteredKeyboards = repository.Get(filter);
//You can also combine two queries togather
var filterdKeyboards2 = repository.Get(keyboard.And(filter))
//Order it on known fields
var orderedKeyboard = keyboard.OrderBy(o=>o.Asc(p=>p.Name));
var orderedKeyboards = repository.Get(orderedKeyboard);
//Or order by on dynamic fields
var userOrdering = keyboard.OrderBy(o=>o.Asc("Name"));
var orderedKeyboards2 = repository.Get(userOrdering);
I do not know about the search object/DTO you're getting but you can easily create a generic search object/DTO and can map it to a Query object in few lines of code. I have used it in past around a WCF service and it has worked very well for me.
add a comment |
up vote
6
down vote
I have created a generic repository which should help you. It supports uniform API to query and sort on both known and dynamic fields:
//Filter on known fields
var keyboard = Query<Product>.Create(p=>p.Category=="Keyboard");
var keyboards = repository.Get(keyboard);
//Or filter on dynamic fields
var filter = Query<Product>.Create("Rating", OperationType.GreaterThan, 4)
var filteredKeyboards = repository.Get(filter);
//You can also combine two queries togather
var filterdKeyboards2 = repository.Get(keyboard.And(filter))
//Order it on known fields
var orderedKeyboard = keyboard.OrderBy(o=>o.Asc(p=>p.Name));
var orderedKeyboards = repository.Get(orderedKeyboard);
//Or order by on dynamic fields
var userOrdering = keyboard.OrderBy(o=>o.Asc("Name"));
var orderedKeyboards2 = repository.Get(userOrdering);
I do not know about the search object/DTO you're getting but you can easily create a generic search object/DTO and can map it to a Query object in few lines of code. I have used it in past around a WCF service and it has worked very well for me.
add a comment |
up vote
6
down vote
up vote
6
down vote
I have created a generic repository which should help you. It supports uniform API to query and sort on both known and dynamic fields:
//Filter on known fields
var keyboard = Query<Product>.Create(p=>p.Category=="Keyboard");
var keyboards = repository.Get(keyboard);
//Or filter on dynamic fields
var filter = Query<Product>.Create("Rating", OperationType.GreaterThan, 4)
var filteredKeyboards = repository.Get(filter);
//You can also combine two queries togather
var filterdKeyboards2 = repository.Get(keyboard.And(filter))
//Order it on known fields
var orderedKeyboard = keyboard.OrderBy(o=>o.Asc(p=>p.Name));
var orderedKeyboards = repository.Get(orderedKeyboard);
//Or order by on dynamic fields
var userOrdering = keyboard.OrderBy(o=>o.Asc("Name"));
var orderedKeyboards2 = repository.Get(userOrdering);
I do not know about the search object/DTO you're getting but you can easily create a generic search object/DTO and can map it to a Query object in few lines of code. I have used it in past around a WCF service and it has worked very well for me.
I have created a generic repository which should help you. It supports uniform API to query and sort on both known and dynamic fields:
//Filter on known fields
var keyboard = Query<Product>.Create(p=>p.Category=="Keyboard");
var keyboards = repository.Get(keyboard);
//Or filter on dynamic fields
var filter = Query<Product>.Create("Rating", OperationType.GreaterThan, 4)
var filteredKeyboards = repository.Get(filter);
//You can also combine two queries togather
var filterdKeyboards2 = repository.Get(keyboard.And(filter))
//Order it on known fields
var orderedKeyboard = keyboard.OrderBy(o=>o.Asc(p=>p.Name));
var orderedKeyboards = repository.Get(orderedKeyboard);
//Or order by on dynamic fields
var userOrdering = keyboard.OrderBy(o=>o.Asc("Name"));
var orderedKeyboards2 = repository.Get(userOrdering);
I do not know about the search object/DTO you're getting but you can easily create a generic search object/DTO and can map it to a Query object in few lines of code. I have used it in past around a WCF service and it has worked very well for me.
edited Jun 20 at 12:00
answered Aug 26 '15 at 16:30
Gurmit Teotia
7914
7914
add a comment |
add a comment |
up vote
1
down vote
You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.
add a comment |
up vote
1
down vote
You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.
add a comment |
up vote
1
down vote
up vote
1
down vote
You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.
You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.
answered Apr 4 '11 at 16:20
Thomas Li
3,0631214
3,0631214
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5541234%2fcreating-dynamic-queries-with-entity-framework%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown