1

I have a problem:

public class PaginationSet
{
    public int TotalItemCount { get; set; }
    public int Page { get; set; }
    public int Amount { get; set; }
    public string Sort { get; set; }
    public string Order { get; set; }

    /// <summary>
    /// This is used to store all the above information in, while still maintaining the automated index count from the internal for loop link builder.
    /// 
    /// Don't forget to pass the index into this!
    /// </summary>
    public Func<int, object> PaginationLinkData
    {
        get
        {
            return index => new
            {
                page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                amount = this.Amount,
                sort = this.Sort,
                order = this.Order
            };
        }
    }
}

this.Sort and this.Order are sometimes null. If they are, I would like to not place them inside the returning Func<int,object>... How do i go about doing this?

It might look something like this:

    public Func<int, object> PaginationLinkData
    {
        get
        {
            Func<int,object> something = index => new
            {
                page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                amount = this.Amount
            };

            if( this.Sort != null ) 
            {
                something.sort = this.Sort,
                something.order= this.Order
            }

            return something;
        }
    }
}
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • I think you can use `Expressions` for this matter to compile a lambda on the fly. – JustSomeGuy Mar 09 '15 at 22:50
  • I am going to take a shot at giving you a link that may help you: http://stackoverflow.com/questions/14663763/how-to-add-an-attribute-to-a-property-at-runtime – Logan B. Lehman Mar 09 '15 at 22:51

3 Answers3

1

Can't you just do something like this?

public Func<int, object> PaginationLinkData
    {
        get
        {
            if( this.Sort != null ) 
            {
                return index => new
                {
                    page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                    amount = this.Amount,
                    sort = this.Sort,
                    order = this.Order
                };
            }
            else
            {
                return index => new
                {
                    page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                    amount = this.Amount,
                };
            }
        }
    }
Darko Kenda
  • 4,781
  • 1
  • 28
  • 31
1

I am guessing you are serializing to JSon somewhere. If so and you can use dynamic why not:

/// <summary>
/// This is used to store all the above information in, while still maintaining the automated index count from the internal for loop link builder.
/// 
/// Don't forget to pass the index into this!
/// </summary>
public Func<int, object> PaginationLinkData
{
    get
    {
        dynamic res = new ExpandoObject();

        res.amount = Amount;
        if (Sort != null) res.sort = Sort;
        if (Order != null) res.order = Order;

        return index =>
        {
            res.page = index;
            return res;
        };
    }
}
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
1

Try using expando object...

public Func<int, object> PaginationLinkData
{
    get
    {
        return index =>
            {
                dynamic obj = new ExpandoObject();
                obj.page = index;
                obj.amount = this.Amount;
                if (this.Sort != null)
                {
                    obj.sort = this.Sort;
                }
                if (this.Order != null)
                {
                    obj.order = this.Order;
                }
                return obj;
            };
    }
}
Dinesh
  • 470
  • 4
  • 13