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

Getting Index of a specific row??

3 Answers 107 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gabriel
Top achievements
Rank 1
Gabriel asked on 04 Dec 2010, 10:58 AM
I've got  a gridview and a persistent class which is binded to the gridview via ObjectView. I want to search for a specific row in a specific condition then focus to that row. For doing that I used the following code to find the specific row, now I want to find the index of the result in the grid so that I can focus on the specific row in the grid:
IObjectScope scope = ObjectScopeProvider1.ObjectScope();
var result = from c in scope.Extent<MyPersistentClass>()
             where c.Id == 1
             select c;

I don't know how to use IndexOf method of ObjectView, every time I use this function I get -1 as a result.
What should I do??

3 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 06 Dec 2010, 06:11 PM
Hello Gabriel,

The reason for this behavior is that the IObjectScope instance that is used for retrieving the persistent object is not the same as the IObjectScope instance used to bind the GridView.
You could use the following approach to work with the correct IObjectScope object:

IObjectScope scope = this.ObjectProvider1.Context as IObjectScope;
if (scope != null)
{
    MyPersistentClass result  = scope.Extent<MyPersistentClass>().FirstOrDefault(x => x.ID == 1);
    if (result  != null)
    {
        int index = this.ObjectView1.IndexOf(result);
        ...
    }
}

Hope that helps.

Regards,
Damyan Bogoev
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
0
Gabriel
Top achievements
Rank 1
answered on 06 Dec 2010, 07:51 PM
Thanks, that worked.
But what if I have multiple results instead of just one result, and I want to navigate trough the results. That's why I used this query:
var result = from c in scope.Extent<MyPersistentClass>()
             where c.Id == 1
             select c;
// or maybe this:
var result = from c in scope.Extent<MyPersistentClass>()
             where c.Name == "some text"
             select c;
0
Damyan Bogoev
Telerik team
answered on 08 Dec 2010, 06:51 PM
Hello Gabriel,

You could store the result from the query within a collection:

List<MyPersistentClass> result = from c in scope.Extent<MyPersistentClass>()
                                 where c.FilterField = filterValue
                                 select c;

Later you will be able to iterate over this list and retrieve the index of the current object from the list using the
approach proposed in the previous post.
Hope that helps.

Greetings,
Damyan Bogoev
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
LINQ (LINQ specific questions)
Asked by
Gabriel
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Gabriel
Top achievements
Rank 1
Share this question
or