Overwrite value in getmulti()

Posts   
 
    
Posts: 97
Joined: 29-Apr-2009
# Posted on: 13-May-2009 08:49:51   

Hello all,

i have following code

CmsArticlesCollection art = new CmsArticlesCollection(); for (int i = 1; i <= 12; i++) { IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)", new object[] { new DbFunctionCall("MONTH", new object[] { CmsArticlesFields.DtCreationDate }) })) == i; art.GetMulti(monthFilter); } GridView1.DataSource = art; GridView1.DataBind();

so final output gives only i=12 value in art collection

so how can i get all the values from i=1 to i=12

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 13-May-2009 09:41:22   

If I understand you correctly:

You need to construct a PredicateExpression inside the for and then pass it to the GetMulti() method which should be called outside of the for loop.

Posts: 97
Joined: 29-Apr-2009
# Posted on: 13-May-2009 10:05:05   

Walaa wrote:

If I understand you correctly:

You need to construct a PredicateExpression inside the for and then pass it to the GetMulti() method which should be called outside of the for loop.

hi walaa, i try like below:

    CmsArticlesCollection art = new CmsArticlesCollection();
    PredicateExpression Filter = new PredicateExpression();
    for (int i = 1; i <= 12; i++)
    {

        IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)",
                   new object[] { new DbFunctionCall("MONTH", new object[] { CmsArticlesFields.DtCreationDate }) })) == i;
        Filter.Add(monthFilter);


    }
     art.GetMulti(Filter);
    GridView1.DataSource = art;
    GridView1.DataBind();

but no output.

can you please correct me.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 13-May-2009 10:14:08   

I'm not sure but I think you need to do this instead:

 Filter.AddWithOr(monthFilter);

You want the predicates OR-ed not And-ed...right?

Please examine the generated SQL for better troubleshooting.

Posts: 97
Joined: 29-Apr-2009
# Posted on: 13-May-2009 11:19:28   

Walaa wrote:

I'm not sure but I think you need to do this instead:

 Filter.AddWithOr(monthFilter);

You want the predicates OR-ed not And-ed...right?

Please examine the generated SQL for better troubleshooting.

Hi walaa,

Thanks for reply. i got solve that problem, but now i am facing one more problem :


CmsArticlesCollection art = new CmsArticlesCollection();
        PredicateExpression Filter = new PredicateExpression();
        for (int year = 2008; year <= 2008; year++)
        {
            for (int i = 1; i <= 12; i++)
            {

                IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)",
                           new object[] { new DbFunctionCall("MONTH", new object[] { CmsArticlesFields.DtCreationDate }) })) == i;
                IPredicate yearFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)",
                           new object[] { new DbFunctionCall("YEAR", new object[] { CmsArticlesFields.DtCreationDate }) })) == year;
                
                Filter.AddWithOr(monthFilter);
                Filter.AddWithAnd(yearFilter);
            }
        }
         art.GetMulti(Filter);
        GridView1.DataSource = art;
        GridView1.DataBind();


now problem is that i have to match year and month, but in above code i am also getting value of year 2009 also.

so how can i implement so i get only 2008 value???

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 13-May-2009 11:48:04   

Try this out:

       CmsArticlesCollection art = new CmsArticlesCollection();
        PredicateExpression Filter = new PredicateExpression();
        for (int year = 2008; year <= 2008; year++)
        {
            for (int i = 1; i <= 12; i++)
            {

                IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)",
                         new object[] { new DbFunctionCall("MONTH", new object[] { CmsArticlesFields.DtCreationDate }) })) == i;
                IPredicate yearFilter = new EntityField("OrderYear", new DbFunctionCall("CAST({0} AS bigint)",
                         new object[] { new DbFunctionCall("YEAR", new object[] { CmsArticlesFields.DtCreationDate }) })) == year;

                PredicateExpression dateFilter = new PredicateExpression();
                dateFilter.Add(monthFilter);
                dateFilter.Add(yearFilter);
                
                Filter.AddWithOr(dateilter);
            }
        }
         art.GetMulti(Filter);
        GridView1.DataSource = art;
        GridView1.DataBind();

btw, why bother filtering with the month if you are looping for all months of the year?

Posts: 97
Joined: 29-Apr-2009
# Posted on: 13-May-2009 12:18:23   

Walaa wrote:

Try this out:

       CmsArticlesCollection art = new CmsArticlesCollection();
        PredicateExpression Filter = new PredicateExpression();
        for (int year = 2008; year <= 2008; year++)
        {
            for (int i = 1; i <= 12; i++)
            {

                IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)",
                         new object[] { new DbFunctionCall("MONTH", new object[] { CmsArticlesFields.DtCreationDate }) })) == i;
                IPredicate yearFilter = new EntityField("OrderYear", new DbFunctionCall("CAST({0} AS bigint)",
                         new object[] { new DbFunctionCall("YEAR", new object[] { CmsArticlesFields.DtCreationDate }) })) == year;

                PredicateExpression dateFilter = new PredicateExpression();
                dateFilter.Add(monthFilter);
                dateFilter.Add(yearFilter);
                
                Filter.AddWithOr(dateilter);
            }
        }
         art.GetMulti(Filter);
        GridView1.DataSource = art;
        GridView1.DataBind();

btw, why bother filtering with the month if you are looping for all months of the year?

hi walaa,

i am this is just i am test for 2008 only, it might be 2008 to 2010 or any year.

like : for (int year = 2008; year <= 2010; year++)

i will just check your solution. thanks for reply.

i am working on small blog system, so i am checking in 2008 in which month how many article posted? same for 2009 and so on...

Posts: 97
Joined: 29-Apr-2009
# Posted on: 13-May-2009 14:24:00   

Walaa wrote:

Try this out:

       CmsArticlesCollection art = new CmsArticlesCollection();
        PredicateExpression Filter = new PredicateExpression();
        for (int year = 2008; year <= 2008; year++)
        {
            for (int i = 1; i <= 12; i++)
            {

                IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)",
                         new object[] { new DbFunctionCall("MONTH", new object[] { CmsArticlesFields.DtCreationDate }) })) == i;
                IPredicate yearFilter = new EntityField("OrderYear", new DbFunctionCall("CAST({0} AS bigint)",
                         new object[] { new DbFunctionCall("YEAR", new object[] { CmsArticlesFields.DtCreationDate }) })) == year;

                PredicateExpression dateFilter = new PredicateExpression();
                dateFilter.Add(monthFilter);
                dateFilter.Add(yearFilter);
                
                Filter.AddWithOr(dateilter);
            }
        }
         art.GetMulti(Filter);
        GridView1.DataSource = art;
        GridView1.DataBind();

btw, why bother filtering with the month if you are looping for all months of the year?

Hi walaa,

Thanks you so much. everything is works fine.

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 13-May-2009 14:28:24   

Still I think you can filter by the years and skip the months filter (skip the inner for loop), unless of-course if you are gonna need to filter on specific months out of the 12.

Posts: 97
Joined: 29-Apr-2009
# Posted on: 14-May-2009 14:04:46   

Walaa wrote:

Still I think you can filter by the years and skip the months filter (skip the inner for loop), unless of-course if you are gonna need to filter on specific months out of the 12.

You Right Walaa,

i have implement it. thanks a lot.