Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq中的变化变量_C#_Linq - Fatal编程技术网

C# linq中的变化变量

C# linq中的变化变量,c#,linq,C#,Linq,我有一个类似这样的问题 function List<CustomObject2> GetDataPoint(List<CustomObject> listDataPoints) { if(listDataPoints.Count == 0) return; var startPoint = new CustomObject(); startPoint = listDataPoint.First(); List<CustomObject2> cObj

我有一个类似这样的问题

function List<CustomObject2> GetDataPoint(List<CustomObject> listDataPoints)
{
 if(listDataPoints.Count == 0)
  return;
 var startPoint = new CustomObject();
 startPoint = listDataPoint.First();
 List<CustomObject2> cObjList = from r in listDataPoints
                                where r != null && r.GetDistance(startPoint) > 100
                                select new CustomObject2
                                {
                                  Var1 = r.Var1
                                }.ToList()
 } 
函数列表GetDataPoint(列表listDataPoints)
{
如果(listDataPoints.Count==0)
返回;
var startPoint=new CustomObject();
startPoint=listDataPoint.First();
在listDataPoints中列出cObjList=来自r
其中r!=null&&r.GetDistance(startPoint)>100
选择新的CustomObject2
{
Var1=r.Var1
}托利斯先生()
} 
这里的问题是,在开始时,startPoint被设置为listDataPoint中的第一个对象。但是,在查询(GetDistance)中进行比较之后,如果距离大于100,我想将startPoint重新指定为“r”的值

有什么办法吗

提前感谢

您可以使用折叠:

var cObjList = listDataPoints.Where(r => r != null)
    .Aggregate(Tuple.Create(startPoint, new List<CustomObject2>()), (acc, r) => {
        if(r.GetDistance(acc.Item1)) {
            acc.Item2.Add(new CustomObject2 { Var1 = r.Var1 });
            return Tuple.Create(r, acc.Item2);
        }
        else return acc;
    }).Item2;
var cObjList=listDataPoints.Where(r=>r!=null)
.Aggregate(Tuple.Create(startPoint,new List()),(acc,r)=>{
如果(r.GetDistance(根据第1项)){
acc.Item2.Add(新的CustomObject2{Var1=r.Var1});
返回Tuple.Create(r,acc.Item2);
}
否则返回acc;
}).项目2;

不,没有干净的方法可以做到这一点

LINQ本质上是一个被引入C#的
函数式编程
。在函数式编程中,值是不可变的(不能更改)。由于LINQ查询具有功能性和不可变性,因此可以对其进行惰性评估。LINQ查询仅部分运行,或者序列的某些部分需要多次计算,这种情况并不少见。由于不变性,这样做是安全的

只要您想要更改一个值,您就可以使用LINQ。在这种情况下,使用for循环会更好


当然,有一些方法可以用函数的方式来解决这个问题,因为可以用纯函数语言来解决这个问题。但是在C#中,使用for循环要干净得多。

因为您没有检查
列表数据点中的元素,所以我假设它可能包含null对象。在这种情况下,当列表中的
First()
元素为空时,代码可能会受到攻击

//there is no function or procedure in c#;
//function List<CustomObject2> GetDataPoint(List<CustomObject> listDataPoints)
List<CustomObject2> GetDataPoint(List<CustomObject> listDataPoints)
{
    var dataPoints = listDataPoints.Where(r => r != null);

    if (dataPoints.Empty())
        //return; you cant not return anything in a function
        return null; //or return an empty list
        //return new List<CustomObject2>();


    var cObjList = dataPoints.Aggregate(
        new Stack<CustomObject>(),
        (results, r) =>
        {   
            if (r.GetDistance(results.Peek()) > 100)
                results.Add(r);
            return results;
        })
        .Select(r => new CustomObject2(){ Var1 = r.Var1 })
        .ToList();

    //return directly the line above or do more work with cObjList...
}
//c#中没有函数或过程;
//函数列表GetDataPoint(列表listDataPoints)
列出GetDataPoint(列出listDataPoints)
{
var dataPoints=listDataPoints.Where(r=>r!=null);
if(dataPoints.Empty())
//return;函数中不能返回任何内容
return null;//或返回空列表
//返回新列表();
var cObjList=dataPoints.Aggregate(
新堆栈(),
(结果,r)=>
{   
如果(r.GetDistance(results.Peek())>100)
结果:添加(r);
返回结果;
})
.Select(r=>newcustomobject2(){Var1=r.Var1})
.ToList();
//直接返回上面的行或使用cObjList执行更多工作。。。
}
然而,这仍然是混乱的,不容易维持。正如Anders Abel所建议的,在这种情况下,最好使用for循环:

var cObjList= new List<CustomObject2>();
foreach(var r in dataPoints)
{
    if (r.GetDistance(results.Peek()) > 100)
        results.Add(new CustomObject2(){ Var1 = r.Var1 });
}

//...

return cObjList;
var cObjList=newlist();
foreach(数据点中的var r)
{
如果(r.GetDistance(results.Peek())>100)
Add(newcustomobject2(){Var1=r.Var1});
}
//...
返回cObjList;

你可以玩这个把戏,让
where r!=null&&r.GetDistance(startPoint)>100&&r(startPoint=r)==r
,但我会选择for循环。