using SunlightAggregationTerminal.Models; using System.Collections.ObjectModel; namespace SunlightAggregationTerminal; public partial class QueryDetail : ContentPage { public static ObservableCollection QueryDetailDyelotDetailsItems { get; set; } = new(); public static ObservableCollection QueryDetailDyelotDetailsItemst { get; set; } = new(); public QueryDetail(DyelotItem item) { InitializeComponent(); _Dyelot.Text = item.Dyelot; _ReDye.Text = item.ReDye?.ToString()??""; _Machine.Text = item.Machine; _CreationTime.Text = item.CreationTime; _FabricName.Text = item.FabricName; _CustomerName.Text = item.CustomerName; _ColorName.Text = item.ColorName; QueryDetailDyelotDetailsItems.Add(new DyelotDetailsItem { Dyelot = "2134566", Machine = "g01", Step="3", CreationTime = "", DispenserGram="32434", Gram = "3212", ReDye = 0, ProductName="sdvsf1", Information = "yyjjy\nfxfxdsdg\n", }); QueryDetailDyelotDetailsItems.Add(new DyelotDetailsItem { Dyelot = "2134566", Machine = "g01", Step = "3", CreationTime = "", DispenserGram = "32423434", Gram = "3212434", ReDye = 0, ProductName = "sdvsf2", Information = "yyjjy\nfxfxdsdg\n", }); QueryDetailDyelotDetailsItems.Add(new DyelotDetailsItem { Dyelot = "2134566", Machine = "g01", CreationTime = "", DispenserGram = "3212434", Gram= "3212434", Step = "4", ReDye = 0, ProductName = "sdvsf3", Information = "yyjjy\nfxfxdsdg\n", }); Data_(); } private void Data_() { var processedList = QueryDetailDyelotDetailsItems .GroupBy(item => new { // 建议加上 ?.Trim() 防止空格和 null 导致的分组偏差 Dyelot = item.Dyelot?.Trim(), Step = item.Step?.Trim() }) .Select(g => { var firstItem = g.First(); // 提取该组内所有的 ProductName 并转为字符串 // 提取该组内所有的 Gram,安全地转为字符串(防止 null 报错) // 将两者用 Zip 方法一一配对,拼接成 "ProductName(Gram)" 的格式 // 如果只想简单的全部堆在一起,也可以直接用 string.Join(",", productNames.Concat(grams)) var productNames = g.Select(x => x.ProductName); var grams = g.Select(x => x.Gram?.ToString()); var dispenserGrams = g.Select(x => x.DispenserGram?.ToString()); // 使用 Zip 将三者一一配对,拼接成 "名称(Gram/dispenserGram)" 的格式 var combinedInfo = string.Join("\n",productNames.Zip(grams, (name, gram) => (name, gram)) // 先把前两个拼成元组 .Zip(dispenserGrams, (pair, dispGram) => $"原料:{pair.name}\n目标用量:{pair.gram}g\n实际用量:{dispGram}g")); // 赋值给最终展示的 ProductName firstItem.ProductName = combinedInfo; return firstItem; }) .ToList(); // 强制立刻执行查询 // 更新 UI 绑定的 ObservableCollection QueryDetailDyelotDetailsItems.Clear(); foreach (var item in processedList) { QueryDetailDyelotDetailsItems.Add(item); } CardCollectionView_data.ItemsSource = QueryDetailDyelotDetailsItems; } }