使用实体框架获取列表中的行索引 [英] Get Row Index in a list by using entity framework
问题描述
我的列表中有5个数据。
I have 5 datas in my list.
我的列表
我想知道,如何获取ID的行号?
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();
例如,
我的列表有以下5个数据,
As instance, My List has 5 datas like below,
Id-Name
6,Computer
135,KeyBoard
68,Mouse
98,Telephone
213,Laptop,
MyProductId
是 135
,因此与Keyboard匹配。它是行数number(index)必须为 1,因为0是计算机。
MyProductId
is 135
so it matchs with Keyboard.It is row count number( index )must be "1" because 0 is Computer.
如何获取ID的行数(索引)编号?
How can i get selected Id's row count(index) number ?
推荐答案
如果您的列表已实现索引器,则可以直接通过Index获得该元素。您会发现如何实现索引器。
You can get it directly the element by Index if your list has implemented indexer. You can find that How to implement Indexer.
另一种方法可以在这里。
上面的链接显示为:
从上方复制链接:
Person agedTwenty = myList.Where<Person>( x => return x.Age == 20; ).Single<Person>();
int index = myList.IndexOf(agedTwenty);
或者
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.
这篇关于使用实体框架获取列表中的行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!