[AR] FindAll(ICriterion[]) returns more results than expected.

72 views
Skip to first unread message

Marco Antonio

unread,
May 17, 2010, 9:19:38 PM5/17/10
to Castle Project Users
Hi all,

I'm having a bad time with FindAll(ICriterion[]). Well, maybe I'm
doing something wrong too...that's more probably to happen. But,
anyway...

I have a method called FilterPostByRequest, in my webapplication. This
method extract all parameters from Request.QueryString and creates a
list of ICriterion to pass to FindAll method, so, it's suposed to
bring back, from database, all records that correspond to the filter.
In my database, the queried table, has 30 records...but when the app
execute the method (FindAll(ICriterion[])) it returns 41 records
(???)...something is not right. Can, anyone, help me with this
question?? Thanks!

Note...FindAll() (without parameters) returns 30 records, as expected.

I'm using MySql, AR 2.1.2, and my models, all collections (lists)
using lazy load.

The method (FilterPostByRequest)

private IEnumerable<Post> FilterPostsByRequest(HttpRequestBase
request, bool onlyPublished)
{
long categoryId = 0;
string title = string.Empty;
DateTime dateCreatedFrom = DateTime.MinValue;
DateTime dateCreatedTo = DateTime.MinValue;
State state = State.None;
IList<ICriterion> criterions = null;

categoryId = request.QueryString.Get<long>("category.Id");
title = request.QueryString.Get<string>("post.Title");
dateCreatedFrom =
request.QueryString.Get<DateTime>("post.DateCreated.From");
dateCreatedTo =
request.QueryString.Get<DateTime>("post.DateCreated.To");
state = (State)request.QueryString.Get<int>("post.State");

criterions = new List<ICriterion>();

if (categoryId != 0)
criterions.Add(Expression.Eq("Category.Id", categoryId));

if (!string.IsNullOrEmpty(title))
criterions.Add(Expression.InsensitiveLike("Title", title));

if (dateCreatedFrom != DateTime.MinValue && dateCreatedTo !=
DateTime.MinValue)
criterions.Add(Expression.Between("DateCreated", dateCreatedFrom,
dateCreatedTo));

if (!onlyPublished)
{
if (state != State.None)
criterions.Add(Expression.Eq("State", state));
}
else
{
criterions.Add(Expression.Eq("State", State.Normal));
}

return Post.FindAll(criterions.ToArray()); // Database has 30
records, but this method return 41 (ex.). FindAll() works fine.
}

p.s.: sorry about the "bad english"! =)

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Krzysztof Koźmic

unread,
May 18, 2010, 1:16:37 AM5/18/10
to castle-pro...@googlegroups.com
You probably have outer join there.
In this case use DistinctRootEntityTransformer, to filter out duplicates.

Krzysztof
> if (dateCreatedFrom != DateTime.MinValue&& dateCreatedTo !=

Marco Teixeira

unread,
May 21, 2010, 8:28:08 PM5/21/10
to castle-pro...@googlegroups.com
Hi Krzysztof,

Thanks! I tried your suggestion and works great.

Thanks again! =)
 
Marco Antonio



De: Krzysztof Koźmic <krzyszto...@gmail.com>
Para: castle-pro...@googlegroups.com
Enviadas: Terça-feira, 18 de Maio de 2010 2:16:37
Assunto: Re: [AR] FindAll(ICriterion[]) returns more results than expected.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Reply all
Reply to author
Forward
0 new messages