Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

c# - Get Row Index in a list by using entity framework

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...