使用' out'在Linq查询中

发布于 2025-02-02 06:22:38 字数 713 浏览 3 评论 0原文

我正在使用list 组件对象,componentList

组件具有方法getPosition通过component.getPosition(out of position,out of arientation)返回组件的位置。 我可以通过position.x,position.y,position.z获得x,y,z。

我有一个单独的list< csvpart>从CSV文件中导入的。每个列表项目也都有X,Y,Z。我想从CSV零件列表中找到匹配X,Y,Z的组件。

我尝试过:

foreach (CSVPart p in csvParts)
{
    foundComponent = componentList
        .Where(c => c.Name == p.PartNumber & ... == p.X & ... == p.Y & ... == p.Z
        )
}

名称与partnumber相对应的地方,...对应于我茫然地盯着屏幕。 我已经尝试将后续语句嵌套以比较{}中的x,y,z,但是我尝试过任何尝试。如何将out结果输入此LINQ查询?事先感谢您的帮助。

I am working with a List of Component objects, componentList.

Component has method GetPosition which returns position of a component via component.GetPosition(out position, out orientation).
I can get X, Y, Z via position.X, position.Y, position.Z.

I have a separate List<CSVPart> imported from a CSV file. Each list item also has X, Y, Z. I want to find Component which matches X, Y, Z from the list of CSV parts.

I have tried:

foreach (CSVPart p in csvParts)
{
    foundComponent = componentList
        .Where(c => c.Name == p.PartNumber & ... == p.X & ... == p.Y & ... == p.Z
        )
}

Where Name corresponds to PartNumber and ... corresponds to me staring blankly at the screen.
I've tried nesting the subsequent statements to compare X, Y, Z in {} but nothing I've tried worked. How do I get the out results into this Linq query? Thanks in advance for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2025-02-09 06:22:38

我建议您不要尝试以单个表达方式进行。相反,要么编写一个方法,该方法可以在查询中使用您想要的匹配并参考该方法,要么使用块状lambda :(

foreach (CSVPart p in csvParts)
{
    var foundComponent = componentList.FirstOrDefault(c =>
    {
        // Avoid finding the position if the name doesn't match.
        if (c.Name != p.PartNumber)
        {
            return false;
        }
        c.GetPosition(out var position, out var _);
        return position.X == p.X && position.Y == p.Y && position.Z == p.Z;
    });
    // foundComponent will be null or the first match
}

我已经从中更改为 firstordOddordEfault顾名思义,您正在尝试找到一个值...)

I would suggest you don't try to do it in a single expression. Instead, either write a method that does the matching you want and refer to that in your query, or use a block-bodied lambda:

foreach (CSVPart p in csvParts)
{
    var foundComponent = componentList.FirstOrDefault(c =>
    {
        // Avoid finding the position if the name doesn't match.
        if (c.Name != p.PartNumber)
        {
            return false;
        }
        c.GetPosition(out var position, out var _);
        return position.X == p.X && position.Y == p.Y && position.Z == p.Z;
    });
    // foundComponent will be null or the first match
}

(I've changed from Where to FirstOrDefault as the name suggests you're trying to find a single value...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文