6

I have 5 datas in my list.

MyList

So i want to know , how can i get selected Id's Row Number ?

var MyProductId= 135;
var SelectedDataRowNumber = context.SP_MyList.ToList()
                                   .Where(s=>s.Id==MyProductId)
                                   .FirstOrDefault();

As instance, My List has 5 datas like below,

Id-Name

6,Computer

135,KeyBoard

68,Mouse

98,Telephone

213,Laptop,

MyProductId is 135 so it matchs with Keyboard.It is row count number( index )must be "1" because 0 is Computer.

How can i get selected Id's row count(index) number ?

3
  • what do you mean by Row number? Can you please add some example data, of what the objects in your list look like? Commented Nov 28, 2013 at 7:09
  • I updated please check Commented Nov 28, 2013 at 7:12
  • duplicate of stackoverflow.com/a/2471631/665689 Commented Nov 28, 2013 at 7:19

2 Answers 2

7

You can get it directly the element by Index if your list has implemented indexer. You can find that How to implement Indexer.

Another way could be as described in Here.

The above link shows it like:

COPIED From Above link:

Person agedTwenty = myList.Where<Person>( x => return x.Age == 20; ).Single<Person>();
int index = myList.IndexOf(agedTwenty);

or alternatively

int index = myList.Where<Person>( x => return x.Age == 20; ).Select<Person,int>( x =>         
myList.IndexOf(x)).Single<int>();

In case there can be more than one result you'd do this:

IEnumerable<Person> allAgedTwenty = myList.Where<Person>( x => return x.Age == 20; );
IEnumerable<int> indices = allAgedTwenty.Select<Person,int>( x => myList.IndexOf(x) );

The first case will get you only one int and the second case will leave you with a list of ints.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want the count you need to groupBy

var SelectedDataRowNumber = context.SP_MyList.ToList()
                                   .GroupBy(x = x.Id)
                                   .Where(s=>s.Key == MyCustomerId )
                                   .Select(x => new {Id = x.Key, Count = x.Count()});

This gives you the output

Id = 135
Count = 3 //depending on the actual count

alternative solution, if you want to output id, name and count:

//"items" is my custom list I created to reflect your data
var SelectedDataRowNumber = from x in items 
                            group x by new {x.Id, x.Name} into g
                            where g.Key.Id == 135
                            select new 
                            {
                                ID = g.Key.Id, 
                                Name = g.Key.Name, 
                                Count = g.Count()
                            };

Regarding your comment: is this the result you are expecting: http://abload.de/img/so1j5rta.jpg ?

2 Comments

SelectedDataRowNumber .Count is always 1 i do not know why
If you take SelectedDataRowNumber.Count() it will be always 1 if you grouped by Id and filtered it by Id afterwards. The count you want is in the result set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.