从 LINQ 查询返回命名元组

问题描述 投票:0回答:3

我一整天都在尝试,也进行了很多次尝试,但运气不佳。我有以下返回结果的 Linq 查询。

此查询返回 IEnumerable:

var temp = from entity in remainingEntities
            where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
            select entity;

此查询返回 (IEnumerable, end)

var temp = (from entity in remainingEntities
            where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
            select entity, ChainMatchType.End);

这些都不是我想要的。最终 IEnumerable 中的每个结果都需要是一个命名元组,每个元组都有一个 ISketchEntity 和 ChainMatchType:

List<(ISketchEntity sketchEntityName, ChainMatchType MatchName)>

我将在不同的时间进行类似的查询三次。

  1. 当某些类型的 ISketchEntities 与 EndPoint 匹配时。
  2. 当某些类型的 ISketchEntities 与 StartPoint 匹配时。
  3. 当某些类型的 ISketchEntities 与 CenterPoint 匹配时。

当我运行每个查询时,我想使用枚举添加结果类型来表示匹配类型。

public enum ChainMatchType
{
    Start,
    End,
    Center
}

我的计划是在返回结果之前将所有三个查询合并为一个结果。

如何格式化 LINQ 以将结果放入命名元组中:

Name            DataType
entity:         ISketchEntity
MatchType:      ChainMatchType

编辑 FuzzyEquals 是一个自定义扩展。它使用 +/- 容差比较两个点。我正在使用 CAD 数据,过去的历史告诉我,有时两个点可能足够接近而相等,但没有完全相同的坐标。

    public static bool FuzzyEqual(this ISketchPoint sp, ISketchPoint other, double tolerance)
    {
        if (
            sp.X > other.X - tolerance && sp.X < other.X + tolerance &&
            sp.Y > other.Y - tolerance && sp.Y < other.Y + tolerance &&
            sp.Z > other.Z - tolerance && sp.Z < other.Z + tolerance
        )
            return true;
        return false;
    }

刚才看到后我想它可以简化为:

    public static bool FuzzyEqual(this ISketchPoint sp, ISketchPoint other, double tolerance)
    {
        return sp.X > other.X - tolerance && sp.X < other.X + tolerance &&
               sp.Y > other.Y - tolerance && sp.Y < other.Y + tolerance &&
               sp.Z > other.Z - tolerance && sp.Z < other.Z + tolerance;
    }
c# linq tuples
3个回答
5
投票
  • 我建议仅使用具有扩展方法的 Linq,并避免使用关键字样式
    from
    in
    where
    select
    • 我承认这我的个人偏好 - 但 C# 的 Linq 关键字功能有点缺乏活力,你几乎总是最终需要使用一些扩展方法(尤其是
      ToList
      Include
      )并且混合关键字和扩展方法样式会使内容更难阅读。
  • 无论如何,您只需要在
    Select
    步骤中添加值元组,它可以是方法参数(尽管您不能轻松参数化值元组成员名称 - 这是可能的,但超出了范围问题)。

像这样:

List<( ISketchEntity sketchEntityName, ChainMatchType matchName )> withName = remainingEntities
    .Where( e => e.EndPoint.FuzzyEqual( matchPoint, _tolerance ) )
    .Select( e => ( sketchEntityName: e, matchName: matchName ) )
    .ToList()

作为参数化方法:

(我不知道

matchPoint
_tolerance
是什么)。

public static List<( ISketchEntity sketchEntityName, ChainMatchType matchName )> Get2( this IEnumerable<ISketchEntity> entities, ChainMatchType matchType )
{
    // Precondition:
    if( entities is null ) throw new ArgumentNullException(nameof(entities));

    return entities 
        .Where( e => e.EndPoint.FuzzyEqual( matchPoint, _tolerance ) )
        .Select( e => ( sketchEntityName: e, matchName: matchType ) )
        .ToList()
}

0
投票

您只需要在所选部分周围加上大括号即可:

var temp = (from entity in remainingEntities
            where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
            select (entity, ChainMatchType.End));

0
投票

这是创建者的很好的解释以及如何解决它:https://learn.microsoft.com/en-us/dotnet/standard/base-types/choosing- Between-anonymous-and-tuple

重要的是ValueTuple。您的情况的解决方案:

var temp = (from entity in remainingEntities
        where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
        select new ValueTuple<T, string>(entity, ChainMatchType.End));
© www.soinside.com 2019 - 2024. All rights reserved.