Filter item by property value

Topics: Developer Forum, Project Management Forum, User Forum
Jun 25, 2008 at 8:15 AM
Hello, I am new to n2 cms. I am wondering how i can get a list of items of which one property value is equal to  the same value?
Jun 25, 2008 at 8:51 AM
Hi,

you might try one of these: http://www.n2cms.com/Documentation/Manipulating%20content/Finding%20content.aspx
Jun 25, 2008 at 9:10 AM
I have a content item BankingProduct as follows:       ID, Name, Bank, Rate...

and I want to display Banking Products information in a GridView.  Also there are some TextBoxes and Dropdownlist to filter these BankingProducts.
Well I want to show all Banks existed in the database in the Dropdownlist,  how can I fulfill this?

Any suggestions?

Thanks
Jun 25, 2008 at 10:11 AM
You can do this by settting an IEnumerable<ContentItem> as the DataSource of the DropDownList.
An other option is to create an IList<ContentItem> with the items you want and loop these, adding to the the DropDownList.

How to create an IList<ContentItem> can be found via the link above.
Search the code for an example of how to create an IEnumerable<ContentItem> (there are examples of adding it as a DataSource as well).
Jun 25, 2008 at 11:24 AM
Thanks very much for your reply. But I am still not sure how to get it done.

As I said above, Bank is a property of  BankingProduct. What I want to display in the Dropdownlist are all the Bank names stored in the BankingProducts(ContentItems). Since I only want to show the Banks rather than the ContentItem itself.

Would you please provide some sample code for me?

Thanks very much.

Jun 25, 2008 at 11:38 AM
Edited Jun 25, 2008 at 11:39 AM
There might be many BankingProducts with the same Bank name, therefore duplicate Bank name wouldn't be allowed when shown in the DropDownList.
Jun 25, 2008 at 1:40 PM


jingbell wrote:
As I said above, Bank is a property of  BankingProduct. What I want to display in the Dropdownlist are all the Bank names stored in the BankingProducts(ContentItems). Since I only want to show the Banks rather than the ContentItem itself.


Hi, there are a lot of samples in the sourcecode of N2, just download it and search!
With the information provided on this page http://www.n2cms.com/Documentation/Manipulating%20content/Finding%20content.aspx just must be able to filter all information in the N2-collections the way you want!
If you want to filter duplicates or other, use a IList<ContentItem> and loop it for example

Create a function
 public static IEnumerable<ContentItem> GetIt()
    {
        return Find.Items.Where.Type.Eq(typeof(BankingProducts)).OrderBy.SortOrder.Asc.Select();
    }

Then use it something like

foreach (ContentItem item in Custom.GetIt())
{
  bool bankAlreadyAdded = false;
  //test if bank already added
  if(!bankAlreadyAdded){
     //add to dropdown
     DropDown.Items.Add(new ListItem((string)(item.GetDetail("Bank") ?? ""),(string)(item.GetDetail("BankID") ?? "")));
  }
}