我对列表中的每个整数进行平方。这是代码。

class SomeIntgs
{
    List<int> newList = new List<int>();

    public List<int> get()
    {
        IEnumerable<int> intrs = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        newList.AddRange(intrs);
        return newList;

    }
}


我在Main()中遇到错误

    SomeIntgs stg = new SomeIntgs();
    var qry = from n in stg.get() where (P => P*P) select n;


错误:“无法将lambda表达式转换为bool类型”。

请帮忙。

 也可以帮助我,在一般情况下如何处理lambda

最佳答案

where子句采用的lambda指定如何匹配IQueryable中的项目。满足您提供的表达式的IQueryable的任何成员都将被返回。 (这就是为什么您的编译器抱怨布尔值的原因)。

正如其他人提到的那样,您可以删除where子句以将列表中的每个项目平方。

var ints = new int []{1,2,3,4,5,6,7,8};
var squares = ints.Select(x => x*x);
var evenSquares = ints.Where(x => (x % 2) == 0).Select(x => x*x); // only square
                                                     //the even numbers in the list

09-30 14:06
查看更多