欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#中获取多个对象list中对象共有的属性项

程序员文章站 2022-04-15 14:17:42
...

场景

有一组数据list<TestDataList>

每一个TestDataList是一个对象,此对象可能有温度数据,也可能没有温度数据。

有温度数据的情况下,温度数据属性又是一个list,可能有温度A、B、C等不同种类。

怎样在这组list<TestDataList>找到共有的温度B。

C#中获取多个对象list中对象共有的属性项

 

这只是针对温度数据,如果压力和BMS情况也是如此。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

 //存储所有辅助通道温度数据的list
                List<List<DataItem>> autxColumnsList = new List<List<DataItem>>();
                //存储所有辅助通道电压数据的list
                List<List<DataItem>> autvColumnsList = new List<List<DataItem>>();
                //存储所有BMS数据的list
                List<List<DataItem>> bmsColumnsList = new List<List<DataItem>>();
                //遍历所有的待比较的实验数据文件
                foreach (TestDataList dataList in Global.Instance.CompTestDataList)
                 {
                        if (dataList.AutxColumns != null && dataList.AutxColumns.Count > 0)
                        {
                            autxColumnsList.Add(dataList.AutxColumns);
                        }
                        else
                        {
                            isAllHaveAut = false;
                        }

                        if (dataList.AuvxColumns != null && dataList.AuvxColumns.Count > 0)
                        {
                            autvColumnsList.Add(dataList.AuvxColumns);
                        }
                        else
                        {
                            isAllHaveAuv = false;
                        }

                        if (dataList.BmsColumns != null && dataList.BmsColumns.Count > 0)
                        {
                            bmsColumnsList.Add(dataList.BmsColumns);
                        }
                        else
                        {
                            isAllHaveBMS = false;
                        }
                    }

                #region 如果待比较的文件中全部都拥有辅助通道温度

                if (isAllHaveAut)
                    {
                        List<DataItem> maxCoutItem = new List<DataItem>();
                        List<DataItem> allHaveItem = new List<DataItem>();
                        foreach (List<DataItem> item in autxColumnsList)
                        {
                            if(item.Count>maxCoutItem.Count)
                            {
                                maxCoutItem = item;
                            }
                        }
                       
                        foreach (DataItem dataItem in maxCoutItem)
                        {
                            bool isAllHavedItem = true;
                            foreach (List<DataItem> item in autxColumnsList)
                            {
                                if (!item.Contains(dataItem))
                                {
                                    isAllHavedItem = false;
                                }
                            }
                            if (isAllHavedItem)
                            {
                                allHaveItem.Add(dataItem);
                            }
                        }

                        foreach (DataItem item in allHaveItem)
                        {
                            ScaleTitleModel model = new ScaleTitleModel();
                            model.TitleKey = item.DataPropertyName;
                            model.Title = item.DisplayName;
                            yRadioGrupItems.Add(model);
                        }
                    }
                #endregion

                #region 如果待比较的文件中全部都拥有辅助通道电压
              
                    if (isAllHaveAuv)
                    {
                        List<DataItem> maxCoutItem = new List<DataItem>();
                        List<DataItem> allHaveItem = new List<DataItem>();
                        foreach (List<DataItem> item in autvColumnsList)
                        {
                            if (item.Count > maxCoutItem.Count)
                            {
                                maxCoutItem = item;
                            }
                        }

                        foreach (DataItem dataItem in maxCoutItem)
                        {
                            bool isAllHavedItem = true;
                            foreach (List<DataItem> item in autvColumnsList)
                            {
                                if (!item.Contains(dataItem))
                                {
                                    isAllHavedItem = false;
                                }
                            }
                            if (isAllHavedItem)
                            {
                                allHaveItem.Add(dataItem);
                            }
                        }

                        foreach (DataItem item in allHaveItem)
                        {
                            ScaleTitleModel model = new ScaleTitleModel();
                            model.TitleKey = item.DataPropertyName;
                            model.Title = item.DisplayName;
                            yRadioGrupItems.Add(model);
                        }
                    }
                #endregion

                #region 如果待比较的文件中全部都拥有辅助通道电压
                    if (isAllHaveBMS)
                    {
                        List<DataItem> maxCoutItem = new List<DataItem>();
                        List<DataItem> allHaveItem = new List<DataItem>();
                        foreach (List<DataItem> item in bmsColumnsList)
                        {
                            if (item.Count > maxCoutItem.Count)
                            {
                                maxCoutItem = item;
                            }
                        }

                        foreach (DataItem dataItem in maxCoutItem)
                        {
                            bool isAllHavedItem = true;
                            foreach (List<DataItem> item in bmsColumnsList)
                            {
                                if (!item.Contains(dataItem))
                                {
                                    isAllHavedItem = false;
                                }
                            }
                            if (isAllHavedItem)
                            {
                                allHaveItem.Add(dataItem);
                            }
                        }

                        foreach (DataItem item in allHaveItem)
                        {
                            ScaleTitleModel model = new ScaleTitleModel();
                            model.TitleKey = item.DataPropertyName;
                            model.Title = item.DisplayName;
                            yRadioGrupItems.Add(model);
                        }
                    }
                    #endregion

 

相关标签: list