How to create chained/deferred query for Find*

31 views
Skip to first unread message

khcha...@gmail.com

unread,
Oct 13, 2014, 9:43:50 PM10/13/14
to sharpre...@googlegroups.com
Hi Guys,

I am struggling to figure out a way to construct query for Find.

basically i would like to have something like


var myquery; //.......... for init query
if (xxxxx)
{
myquery = xxxxx AND YYYYY
}
else
{
myquery = XXXXX
}

mySharpRepo.FindAll(query);


any idea?
in particular, i don't know what data Type should i use for the myquery

Jeff Treuting

unread,
Oct 14, 2014, 12:51:05 AM10/14/14
to khcha...@gmail.com, sharpre...@googlegroups.com
The Type of the query depends on the type of the repository. So if you have

IRepository<User> mySharpRepo

Then the conditional for building your query could look like this:

Expression<Func<User, bool>> query = null;

if (true)
{
query = x => x.IsBasicUser;
}
else
{
query = x => x. IsBasicUser && x.UserTypeId == 1;
}

mySharpRepo.FindAll(query);

There are other ways to do it as well but that would work.

Jeff
--
You received this message because you are subscribed to the Google Groups "SharpRepository" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sharpreposito...@googlegroups.com.
To post to this group, send email to sharpre...@googlegroups.com.
Visit this group at http://groups.google.com/group/sharprepository.
To view this discussion on the web visit https://groups.google.com/d/msgid/sharprepository/9a0c1305-e952-47e9-b0bd-767e9c9cf14b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

khcha...@gmail.com

unread,
Oct 14, 2014, 12:57:52 AM10/14/14
to sharpre...@googlegroups.com, khcha...@gmail.com
I see. didn't know you can create lambda expression like that.

how about if i want to chain query together base on condition?

for example:

Expression<Func<User, bool>> query = null;

if (filterByType == true)
{
query = z => z.Type == APPLE
}

if (filterByCountry == True)
{
AND query = z => z.Country == AUSTRALIA
}

So that if filterByCountry and filterByType are both true, the query should become something like
z => z.Type == APPLE && z.Country == AUSTRALIA

and if only filterByCountry is set, then the query should be
z => z.Country == AUSTRALIA

Any thought?

Jeff Treuting

unread,
Oct 14, 2014, 1:12:05 AM10/14/14
to khcha...@gmail.com, sharpre...@googlegroups.com
You can do it with expressions simply like:

Expression<Func<User,bool>> query = x => x.IsActive;

If(filterByType)
{
query = query.AndAlso(x => x.Type == "APPLE");
}

So you can just compose new expressions by combining them with other expressions using And, Or, Not, etc.
To view this discussion on the web visit https://groups.google.com/d/msgid/sharprepository/ef893325-b910-4cfb-be32-7b39aa1f4c2d%40googlegroups.com.

Nelson Chan

unread,
Oct 14, 2014, 2:03:04 AM10/14/14
to Jeff Treuting, sharpre...@googlegroups.com
Thank you Jeff, you totally saved my day.

i spent the whole morning looking into SharpRepository.Specification and hitting walls.


Regards,
Nelson Chan

You received this message because you are subscribed to a topic in the Google Groups "SharpRepository" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sharprepository/pGv4A9F8VMU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sharpreposito...@googlegroups.com.

To post to this group, send email to sharpre...@googlegroups.com.
Visit this group at http://groups.google.com/group/sharprepository.
Reply all
Reply to author
Forward
0 new messages