C# - LINQ First() 和隐式引用?
C# - LINQ First() and an implicit reference?
我有一些代码,乍一看对我来说毫无意义。
问题:这里是否存在某种隐式引用?似乎只添加了不匹配的项目,'if' 的第一部分什么也没做。我说得对吗? (我已将我的评论 (///) 添加到我理解的内容中以解决我的问题)。 (//) 注释为原代码注释
public static List<BAPresModel> MergeBudgetApprovalByProgramPMLists(
List<BAPresModel> sourceList, List<BAPresModel> destList)
{
/// we create this and use it but it seems to never impact destList that I can tell
BAPresModel dstBA = new BAPresModel();
// Merge sourceList into destList.
foreach (BAPresModel sl in sourceList)
{
var qryDestList = from dl in destList
where sl.FiscalYearID == dl.FiscalYearID
where sl.X == dl.X
where sl.Y == dl.Y
select dl;
if (qryDestList.Count() > 0) // we found a match in the destination list.
{
/// I researched -First()- but don't see anything clarifying my question
/// Is this creating a reference back to destList from dstBA somehow?
dstBA = qryDestList.First();
/// this applies the math (adding sl.X to dstBA.X )
dstBA.X = Maths.AddNullableDecimals(dstBA.X, sl.X);
dstBA.Y = Maths.AddNullableDecimals(dstBA.Y, sl.Y);
}
else // no match, so add the sourceList item to the destination list.
{
destList.Add(sl);
}
}
/// as far as I can tell the only thing this method actually does is return the original
/// destList with 'sl' matched items added to it
/// all of the work on dstBA is useless??
return destList;
}
感谢您的宝贵时间和评价
假设 BPAPresModel 是 class
dstBA = qryDestList.First();
有效地获取指向该查询返回的第一个匹配条目的指针。
/// this applies the math (adding sl.X to dstBA.X )
dstBA.X = Maths.AddNullableDecimals(dstBA.X, sl.X);
dstBA.Y = Maths.AddNullableDecimals(dstBA.Y, sl.Y);
所以这两行在原来的 destList
中改变了它
Class 类型在 c# 中称为 'reference' 类型。因为在
这样的事情中
BAPresModel foo = SomeFunction();
foo 是对堆上实际对象的引用。如果您是 C 或 C++ 开发人员,请思考
BAPresModel *foo = SomeFunction();
所以不是 'implicit' 参考,而是 'actual' 参考
我有一些代码,乍一看对我来说毫无意义。
问题:这里是否存在某种隐式引用?似乎只添加了不匹配的项目,'if' 的第一部分什么也没做。我说得对吗? (我已将我的评论 (///) 添加到我理解的内容中以解决我的问题)。 (//) 注释为原代码注释
public static List<BAPresModel> MergeBudgetApprovalByProgramPMLists(
List<BAPresModel> sourceList, List<BAPresModel> destList)
{
/// we create this and use it but it seems to never impact destList that I can tell
BAPresModel dstBA = new BAPresModel();
// Merge sourceList into destList.
foreach (BAPresModel sl in sourceList)
{
var qryDestList = from dl in destList
where sl.FiscalYearID == dl.FiscalYearID
where sl.X == dl.X
where sl.Y == dl.Y
select dl;
if (qryDestList.Count() > 0) // we found a match in the destination list.
{
/// I researched -First()- but don't see anything clarifying my question
/// Is this creating a reference back to destList from dstBA somehow?
dstBA = qryDestList.First();
/// this applies the math (adding sl.X to dstBA.X )
dstBA.X = Maths.AddNullableDecimals(dstBA.X, sl.X);
dstBA.Y = Maths.AddNullableDecimals(dstBA.Y, sl.Y);
}
else // no match, so add the sourceList item to the destination list.
{
destList.Add(sl);
}
}
/// as far as I can tell the only thing this method actually does is return the original
/// destList with 'sl' matched items added to it
/// all of the work on dstBA is useless??
return destList;
}
感谢您的宝贵时间和评价
假设 BPAPresModel 是 class
dstBA = qryDestList.First();
有效地获取指向该查询返回的第一个匹配条目的指针。
/// this applies the math (adding sl.X to dstBA.X )
dstBA.X = Maths.AddNullableDecimals(dstBA.X, sl.X);
dstBA.Y = Maths.AddNullableDecimals(dstBA.Y, sl.Y);
所以这两行在原来的 destList
中改变了它Class 类型在 c# 中称为 'reference' 类型。因为在
这样的事情中 BAPresModel foo = SomeFunction();
foo 是对堆上实际对象的引用。如果您是 C 或 C++ 开发人员,请思考
BAPresModel *foo = SomeFunction();
所以不是 'implicit' 参考,而是 'actual' 参考