开发者

Linq on windows phone

开发者 https://www.devze.com 2023-03-12 10:24 出处:网络
It appears I can use the .Where, .First, etc linq expressions in a Windows Phone 7 class library, but not Contains or FindIndex.Are they rea开发者_运维知识库lly not available at all, or is there somet

It appears I can use the .Where, .First, etc linq expressions in a Windows Phone 7 class library, but not Contains or FindIndex. Are they rea开发者_运维知识库lly not available at all, or is there something else I need to include to access them?


You should be able to use Contains, but FindIndex isn't part of LINQ - it's a method on List<T> normally. However, it's not part of List<T> in Silverlight.

If you're having trouble with Contains, please show a piece of code which is failing.


Contains already exists in WP7

System.Linq.Enumerable.Contains

For FindIndex, a work arround like this should be sufficient

var index = YourList.IndexOf(YourList.FirstOrDefault(selector));


For FindIndex, you can create the method in a class helper:

public static int FindIndex<TSource>(this List<TSource> list, Func<TSource, bool> match)
{
    return list.IndexOf(list.FirstOrDefault(match));
}

Then it will work normally.

0

精彩评论

暂无评论...
验证码 换一张
取 消