我有一个IList:
IList list = CallMyMethodToGetIList();
Run Code Online (Sandbox Code Playgroud)
我不知道我能得到它的类型
Type entityType = list[0].GetType();`
Run Code Online (Sandbox Code Playgroud)
我想用LINQ搜索这个列表:
var itemFind = list.SingleOrDefault(MyCondition....);
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助.
T-m*_*oty 25
简单:
IList list = MyIListMethod();
var item = list
.Cast<object>()
.SingleOrDefault(i => i is MyType);
Run Code Online (Sandbox Code Playgroud)
要么:
IList list = MyIListMethod();
var item = list
.Cast<object>()
.SingleOrDefault(i => i != null);
Run Code Online (Sandbox Code Playgroud)
希望这个帮助!
IList list = ...
// if all items are of given type
IEnumerable<YourType> seq = list.Cast<YourType>().Where(condition);
// if only some of them
IEnumerable<YourType> seq = list.OfType<YourType>().Where(condition);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37583 次 |
最近记录: |