首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >解析2个列表并创建一个对照表

解析2个列表并创建一个对照表
EN

Stack Overflow用户
提问于 2019-05-23 01:15:00
回答 1查看 42关注 0票数 0

我在一个屏幕上工作,基本上提供了2个lists之间的比较。

我有一个类RateFactorItem,它包含一个Name属性和一个ProductValues列表。

代码语言:javascript
复制
public class RateFactorItem
    {
        public string Name { get; set; }

        public List<ProductValues> ProductValues { get; set; }
    }

ProductValues类的定义如下:

代码语言:javascript
复制
public class ProductValues
    {
        public string ProductName { get; set; }
        public double? Value { get; set; }
    }

我还有一个名为CompareViewModel的类,我用它来填充compare屏幕。

代码语言:javascript
复制
public class CompareViewModel
    {
        public string ColumnName { get; set; }
        public double? VersionA { get; set; }
        public double? VersionB { get; set; }
        public double? Variance { get; set; }
    }

现在我有两个使用一些方法填充的RateFactorItem列表,示例代码写在下面。

代码语言:javascript
复制
public static List<RateFactorItem> GetRecordsA()
        {
            var items = new List<RateFactorItem>
            {
                new RateFactorItem
                {
                    Name = "Item1",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=200},
                    new ProductValues{ ProductName="product2", Value=300},
                    new ProductValues{ ProductName="product3", Value=400},
                    new ProductValues{ ProductName="product4", Value=500},
                    new ProductValues { ProductName = "product5", Value = 1000 },
                }
                },

                new RateFactorItem
                {
                    Name = "Item2",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=250},
                    new ProductValues{ ProductName="product2", Value=350},
                    new ProductValues{ ProductName="product3", Value=450},
                    new ProductValues{ ProductName="product4", Value=550},
                    new ProductValues { ProductName = "product5", Value = 1050 },
                }
                },

                new RateFactorItem
                {
                    Name = "Item3",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=2300},
                    new ProductValues{ ProductName="product2", Value=3030},
                    new ProductValues{ ProductName="product3", Value=4040},
                    new ProductValues{ ProductName="product4", Value=5030},
                    new ProductValues { ProductName = "product5", Value = 1400 },
                }
                },

                new RateFactorItem
                {
                    Name = "ItemX",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=20},
                    new ProductValues{ ProductName="product2", Value=30},
                    new ProductValues{ ProductName="product3", Value=40},
                    new ProductValues{ ProductName="product4", Value=50 },
                    new ProductValues { ProductName = "product5", Value = 60 },
                }
                }
            };

            return items;
        }

        public static List<RateFactorItem> GetRecordsB()
        {
            var items = new List<RateFactorItem>
            {
                new RateFactorItem
                {
                    Name = "Item1",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=230},
                    new ProductValues{ ProductName="product2", Value=340},
                    new ProductValues{ ProductName="product3", Value=470},
                    new ProductValues{ ProductName="product4", Value=590},
                    new ProductValues { ProductName = "product5", Value = 1010 },
                }
                },

                new RateFactorItem
                {
                    Name = "Item2",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=220},
                    new ProductValues{ ProductName="product2", Value=370},
                    new ProductValues{ ProductName="product3", Value=400},
                    new ProductValues{ ProductName="product4", Value=510},
                    new ProductValues { ProductName = "product5", Value = 150 },
                }
                },

                new RateFactorItem
                {
                    Name = "Item3",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=2900},
                    new ProductValues{ ProductName="product2", Value=3930},
                    new ProductValues{ ProductName="product3", Value=4940},
                    new ProductValues{ ProductName="product4", Value=5930},
                    new ProductValues { ProductName = "product5", Value = 1900 },
                }
                },

                new RateFactorItem
                {
                    Name = "ItemY",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=40},
                    new ProductValues{ ProductName="product2", Value=80},
                    new ProductValues{ ProductName="product3", Value=90},
                    new ProductValues{ ProductName="product4", Value=60 },
                    new ProductValues { ProductName = "product5", Value = 70 },
                }
                }
            };

            return items;
        }

在控制器操作方法中,我尝试创建一个CompareViewModel列表并填充UI,如下所示:

代码语言:javascript
复制
public IActionResult Index()
        {
            var model = new List<CompareViewModel>();

            var productNameA = "product1";
            var productNameB = "product1";

            var recordsA = Records.GetRecordsA();
            var recordsB = Records.GetRecordsB();

            var countA = recordsA.Count();
            var countB = recordsB.Count();

            for (int i = 0; i < Math.Max(countA, countB); i++)
            {
                var itemName = countA > countB ? recordsA[i].Name : recordsB[i].Name;
                var recordA = recordsA.Where(x => x.Name == itemName)?.FirstOrDefault();
                var recordB = recordsB.Where(x => x.Name == itemName)?.FirstOrDefault();

                var subModel = new CompareViewModel
                {
                    ColumnName = itemName,
                    VersionA = recordA != null ? (recordA.ProductValues.Where(x => x.ProductName == productNameA).FirstOrDefault()?.Value ?? 0) : 0,
                    VersionB = recordB != null ? (recordB.ProductValues.Where(x => x.ProductName == productNameB).FirstOrDefault()?.Value ?? 0) : 0,
                    Variance = ((recordA != null ? (recordA.ProductValues.Where(x => x.ProductName == productNameA).FirstOrDefault()?.Value ?? 0) : 0) - (recordB != null ? (recordB.ProductValues.Where(x => x.ProductName == productNameB).FirstOrDefault()?.Value ?? 0) : 0))
                };
                model.Add(subModel);
            }

            return View(model);
        }

出于演示目的,我将recordsArecordsB中的项目数量设置为相同,但在实时情况下,项目数量可以不同。

在上面的示例中,比较屏幕填充如下:

屏幕上显示了常见项目Item1、Item2、Item3,而屏幕上只显示了1个不常见项目。我希望所有常见的和不常见的记录都应该显示在网格中,显然不常见的记录对于它们的对应物会有一个零,如下所示:

因此,如果list1有28条记录,列表2有32条记录,其中有10条是常见的,那么生成的比较屏幕应该显示两个列表中的10条常见记录和不常见的记录。

我尝试使用IntersectExceptConcat运算符,但未能达到预期效果。请注意,RateFactorItem没有Id字段,因为这些字段从未存储在数据库中,所有这些都是动态计算并显示在屏幕上的。

请使用指针进行协助。

EN

回答 1

Stack Overflow用户

发布于 2019-05-23 02:24:10

我使用结果构建了一个DataTable

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            List<RateFactorItem> recordA = GetRecordsA();
            List<RateFactorItem> recordB = GetRecordsB();

            var results = (from a in recordA
                           join b in recordB on a.Name equals b.Name
                           select new { a = a, b = b, name = a.Name}
                           )
                           .Select(x => (from ap in x.a.ProductValues
                                         join bp in x.b.ProductValues on ap.ProductName equals bp.ProductName into abp
                                         from bp in abp.DefaultIfEmpty()
                                         select new { ap = ap, bp = bp, name = x.name }
                                         ).ToList()
                           ).ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add("Item", typeof(string));
            dt.Columns.Add("product", typeof(string));
            dt.Columns.Add("A Quantity", typeof(int));
            dt.Columns.Add("B Quantity", typeof(int));
            dt.Columns.Add("Variance", typeof(int));

            foreach (var item in results)
            {
                foreach (var product in item)
                {
                    DataRow newRow = dt.Rows.Add(new object[] {
                        product.name,
                        product.ap.ProductName,
                        product.ap.Value,
                        product.bp.Value,
                        (product.bp.Value == null) ? (int)product.ap.Value : Math.Abs((int)product.ap.Value - (int)product.bp.Value)
                    });

                }
            }
            dataGridView1.DataSource = dt;

        }
        public static List<RateFactorItem> GetRecordsA()
        {
            var items = new List<RateFactorItem>
            {
                new RateFactorItem
                {
                    Name = "Item1",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=200},
                    new ProductValues{ ProductName="product2", Value=300},
                    new ProductValues{ ProductName="product3", Value=400},
                    new ProductValues{ ProductName="product4", Value=500},
                    new ProductValues { ProductName = "product5", Value = 1000 },
                }
                },

                new RateFactorItem
                {
                    Name = "Item2",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=250},
                    new ProductValues{ ProductName="product2", Value=350},
                    new ProductValues{ ProductName="product3", Value=450},
                    new ProductValues{ ProductName="product4", Value=550},
                    new ProductValues { ProductName = "product5", Value = 1050 },
                }
                },

                new RateFactorItem
                {
                    Name = "Item3",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=2300},
                    new ProductValues{ ProductName="product2", Value=3030},
                    new ProductValues{ ProductName="product3", Value=4040},
                    new ProductValues{ ProductName="product4", Value=5030},
                    new ProductValues { ProductName = "product5", Value = 1400 },
                }
                },

                new RateFactorItem
                {
                    Name = "ItemX",
                    ProductValues = new List<ProductValues>
                {
                    new ProductValues{ ProductName="product1", Value=20},
                    new ProductValues{ ProductName="product2", Value=30},
                    new ProductValues{ ProductName="product3", Value=40},
                    new ProductValues{ ProductName="product4", Value=50 },
                    new ProductValues { ProductName = "product5", Value = 60 },
                }
                }
            };

            return items;
        }

        public static List<RateFactorItem> GetRecordsB()
        {
            var items = new List<RateFactorItem>
            {
                new RateFactorItem
                {
                    Name = "Item1",
                    ProductValues = new List<ProductValues>
                    {
                        new ProductValues{ ProductName="product1", Value=230},
                        new ProductValues{ ProductName="product2", Value=340},
                        new ProductValues{ ProductName="product3", Value=470},
                        new ProductValues{ ProductName="product4", Value=590},
                        new ProductValues { ProductName = "product5", Value = 1010 },
                    }
                },

                new RateFactorItem
                {
                    Name = "Item2",
                    ProductValues = new List<ProductValues>
                    {
                        new ProductValues{ ProductName="product1", Value=220},
                        new ProductValues{ ProductName="product2", Value=370},
                        new ProductValues{ ProductName="product3", Value=400},
                        new ProductValues{ ProductName="product4", Value=510},
                        new ProductValues { ProductName = "product5", Value = 150 },
                    }
                },

                new RateFactorItem
                {
                    Name = "Item3",
                    ProductValues = new List<ProductValues>
                    {
                        new ProductValues{ ProductName="product1", Value=2900},
                        new ProductValues{ ProductName="product2", Value=3930},
                        new ProductValues{ ProductName="product3", Value=4940},
                        new ProductValues{ ProductName="product4", Value=5930},
                        new ProductValues { ProductName = "product5", Value = 1900 },
                    }
                    },

                new RateFactorItem
                {
                    Name = "ItemY",
                    ProductValues = new List<ProductValues>
                    {
                        new ProductValues{ ProductName="product1", Value=40},
                        new ProductValues{ ProductName="product2", Value=80},
                        new ProductValues{ ProductName="product3", Value=90},
                        new ProductValues{ ProductName="product4", Value=60 },
                        new ProductValues { ProductName = "product5", Value = 70 },
                    }
                 }
            };

            return items;
        }
    }
    public class RateFactorItem
    {
        public string Name { get; set; }

        public List<ProductValues> ProductValues { get; set; }
    }
    public class ProductValues
    {
        public string ProductName { get; set; }
        public double? Value { get; set; }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56261924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档