You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用LINQ基于双元素条件转换List<>并合并同Y轴文本?

使用LINQ合并Google Cloud Vision同Y轴文本

直接在现有LINQ链中添加分组和投影逻辑即可实现需求,完整代码如下:

var words = apiResult
             .Skip(1)
             .OrderBy(x => x.BoundingPoly.Vertices[0].Y)
             .GroupBy(x => x.BoundingPoly.Vertices[0].Y)
             .Select(g => new 
             {
                 Text = string.Join(" ", g.Select(item => item.Description)),
                 Y = g.Key
             })
             .ToList();

逻辑说明:

  1. GroupBy(x => x.BoundingPoly.Vertices[0].Y):将排序后的结果按Y轴值分组,把同一行的文本元素归为一组
  2. Select(...):对每个分组进行投影处理:
    • 用string.Join(" ", ...)把组内所有元素的Description(对应示例中的text属性)用空格拼接成完整句子
    • 保留分组的Y轴键值作为合并后对象的Y属性

自定义实体类适配

如果需要返回自定义实体类而非匿名类型,替换投影部分即可:

.Select(g => new MergedTextEntity
{
    MergedText = string.Join(" ", g.Select(item => item.Description)),
    YCoordinate = g.Key
})

示例对应

输入(简化后的EntityAnnotation结构):

{ Description: 'Hi', BoundingPoly.Vertices[0].Y: 22 }
{ Description: 'There!', BoundingPoly.Vertices[0].Y: 22 }
{ Description: 'Bye!', BoundingPoly.Vertices[0].Y: 57 }

输出结果:

{ Text: 'Hi There!', Y: 22 }
{ Text: 'Bye!', Y: 57 }

内容的提问来源于stack exchange,提问作者Nick Blaine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 02:06:27