This is a migrated thread and some comments may be shown as answers.

Filtering DomainDataSource Example

5 Answers 92 Views
DataFilter
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 20 Aug 2010, 02:40 PM
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.

5 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 20 Aug 2010, 02:43 PM
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
0
Rossen Hristov
Telerik team
answered on 20 Aug 2010, 02:48 PM
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
0
Lee
Top achievements
Rank 1
answered on 20 Aug 2010, 03:05 PM
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.
0
Accepted
Rossen Hristov
Telerik team
answered on 20 Aug 2010, 03:22 PM
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
0
Lee
Top achievements
Rank 1
answered on 20 Aug 2010, 03:33 PM
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.
Tags
DataFilter
Asked by
Lee
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Lee
Top achievements
Rank 1
Share this question
or