Filtering DomainDataSource Example

6 posts, 1 answers
  1. Lee Weisenberger
    Lee Weisenberger avatar
    72 posts
    Member since:
    Nov 2009

    Posted 20 Aug 2010 Link to this post

    I'm trying to use the Filtering DomainDataSource Example as a starting point for including RadDataFilter in my application, and I've ran into an issue.

    In the example code there is the following line:
    Type entityType = this.domainDataSource.DomainContext.DomainClient.EntityTypes.First();

    This works because in your example, the "First()" entity type is the Customer entity object.

    In my code, I'm using over 50 entity types and the one that I want to use with the domain data source is somewhere in the middle of the list, not the first.

    What is the syntax that I need to use to return the entity type of any of the entity types in the middle of the list?  Everything I've tried hasn't worked so far.

    Thanks for your help.
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 20 Aug 2010 Link to this post

    Hi Lee Weisenberger,

    I am not sure what do you mean by "in the middle of". EntityTypes is a collection so you can get it by index if you know it or you can use the .Where() and find it by name.

    I hope this helps.

    Kind regards,
    Ross
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 20 Aug 2010 Link to this post

    Hello Lee Weisenberger,

    A quick follow up. It is IEnumerable so you must transform it to list by calling .ToList(). From then on you will have a list that supports indexing and you find the exact item that you need.

    To learn more about extension methods please read this article. They are quite powerful!

    Kind regards,
    Ross
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Lee Weisenberger
    Lee Weisenberger avatar
    72 posts
    Member since:
    Nov 2009

    Posted 20 Aug 2010 Link to this post

    What I meant by "in the middle of" is that it's not the first entity and it's not the last entity in the collection.

    Is it possible to show me, using the example line I included earlier except the Customer entity is not necessarily the first entity in the data context, what the line of code would look like?  I have been unable to get the correct syntax for using .Where as it relates to this problem.

    Thank you.
  6. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 20 Aug 2010 Link to this post

    Hi Lee Weisenberger,

    In .Net if you have a list you can access its items by index. In my previous post I have mentioned to transform the EntityTypes enumerable to a list, so you can access individual elements by their index.
    When you have a list you are not limited to the first or last elements. The First() and Last() extension methods are just a handy utility for getting them.

    So here is what I meant in my previous post.

    using System.Linq;
    using System.Windows.Controls;
     
    namespace SilverlightApplication2
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
     
                var entityType = this.dds.DomainContext.DomainClient.EntityTypes.ToList()[33];
            }
        }
    }

    This will transform the EntityTypes to a List. Once you have the List you can use its indexer (the square brackets) to get a specific item.

    If you take a look at the link I provided you can see that you can use many other extension methods. For example, if you know the name of the entity type you can do this and get it, instead of using the indexer:

    using System.Linq;
    using System.Windows.Controls;
     
    namespace SilverlightApplication2
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
     
                var entityType = this.dds.DomainContext.DomainClient.EntityTypes
                    .ToList()
                    .Where(type => type.Name == "EntityTypeName")
                    .FirstOrDefault();
            }
        }
    }

    Please, read the following articles before going on further. This will help you understand what is going on in the source code that I have pasted above:

    Working with List Objects and Collections
    Extension Methods
    Lambda Expressions
    Enumerable Methods

    I hope this helps.

    Kind regards,
    Ross
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Lee Weisenberger
    Lee Weisenberger avatar
    72 posts
    Member since:
    Nov 2009

    Posted 20 Aug 2010 Link to this post

    Thanks.  That was exactly what I was looking for.

    I was using the indexing method myself, but I knew this wasn't the best solution because the index would change as I added or removed entity objects.

    I was having problems with the .Where clause because I was putting it before the .ToList(), which wouldn't compile (obviously), but I didn't realize that I needed the .ToList() first.  On top of that, I was missing the .FirstOrDefault as well, which, as it turns out, seems to be the big piece I was missing.

    I'll definitely check out the links you posted as well.

    Thanks again.
Back to Top
DevCraft banner