首页 > 解决方案 > 加入 2 名单一个作为另一个的子列表

问题描述

我有两个列表,它们使用 dapper lstPart 和 lstSite 从 SQL Server 数据库中的两个单独的表中返回。两个列表都有一个相互对应的 ITEMNUM 字段。在部件列表中,ITEMNUM 字段是唯一的,但在站点列表中不是。有些部分有多个站点,有些则没有。

using System;

namespace CWIC_Data_Framework.Definition.MP2Definitions
{
    public class MP2BaseTableDefinition

    {
        public string ITEMNUM { get; set; }
        public string? DESCRIPTION { get; set; }
        public string? NOTES { get; set; }
        public string? UOM { get; set; }
        public DateTime? DATEADDED { get; set; }
        public DateTime? LASTEDITDATE { get; set; }
        public string? COMMENTS { get; set; }
        public string? SITE { get; set; }
        public string? LOCATION { get; set; }
        public int? QTYONHAND { get; set; }
    }
}

using System;

namespace CWIC_Data_Framework.Definition.MP2Definitions
{
    public class MP2SiteTableDefinition

    {
        public string ITEMNUM { get; set; }
        public int? REORDERPOINT { get; set; }
        public int? REORDERQTY { get; set; }
        public string? SITE { get; set; }
        public DateTime? LASTDATECOUNTED { get; set; }
        public DateTime? LASTDATERCVD { get; set; }
        public DateTime? LASTACTIVITY { get; set; }
    }
}

我正在使用 C# WPF 创建一个应用程序,我希望得到与此类似的最终结果。 我正在寻找的示例

我想我需要创建一个包含站点列表的单个列表,作为使用 LINQ 的部件列表的子列表,然后将其绑定到 Datagrid,但我并不肯定。

    public class Part
    {
        public int PartID { get; set; }
        public string Description { get; set; }
        public list<site> Location { get; set; }
   }

任何帮助或方向将不胜感激。

这是调用列表(零件列表)之一的类,另一个是相同的。

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using CWIC_Data_Framework.Definition.MP2Definitions;
using Dapper;
using SerilogTimings;

namespace CMMS_Status.Data
{
    internal class MP2BaseTableData

    {
        private static string ThisMethod;
        private string ThisClass;

        public List<MP2BaseTableDefinition> ListOfMp2Base(Dictionary<string, object> paramDictionary)
        {
            ThisMethod = MethodBase.GetCurrentMethod().Name;
            using (var op = Operation.Begin("{Class}{Method}", ThisClass, ThisMethod))

            using (IDbConnection connection = new SqlConnection(Helper.ConnectionValue("CreeMesRtp")))
            {
                var parameters = new DynamicParameters(paramDictionary);
                var SQL = @"
                            Select
                            CMMSSB.MP2_INVY.ITEMNUM,
                            CMMSSB.MP2_INVY.DESCRIPTION,
                            CMMSSB.MP2_INVY.UOM,
                            CMMSSB.MP2_INVY.NOTES,
                            CMMSSB.MP2_INVY.DATEADDED,
                            CMMSSB.MP2_INVY.LASTEDITDATE,
                            CMMSSB.MP2_INVCOMM.COMMENTS,
                            CMMSSB.MP2_STOCK.SITE,
                            CMMSSB.MP2_STOCK.LOCATION,
                            CMMSSB.MP2_STOCK.QTYONHAND
                            From
                            CMMSSB.MP2_INVY Inner Join
                            CMMSSB.MP2_STOCK On CMMSSB.MP2_STOCK.ITEMNUM = CMMSSB.MP2_INVY.ITEMNUM Left Join
                            CMMSSB.MP2_INVCOMM On CMMSSB.MP2_INVCOMM.ITEMNUM = CMMSSB.MP2_INVY.ITEMNUM
                            ";

                var Output = connection.Query<MP2BaseTableDefinition>(SQL, parameters).ToList();
                op.Complete();
                return Output;
            }
        }
    }
}

用于拉取站点信息的类。

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using CMMS_Status.Definition;
using CWIC_Data_Framework.Definition.MP2Definitions;
using Dapper;
using SerilogTimings;

namespace CMMS_Status.Data
{
    internal class MP2SiteTableData

    {
        private static string ThisMethod;
        private string ThisClass;

        public List<MP2SiteTableDefinition> ListOfMp2Site(Dictionary<string, object> paramDictionary)
        {
            ThisMethod = MethodBase.GetCurrentMethod().Name;
            using (var op = Operation.Begin("{Class}{Method}", ThisClass, ThisMethod))

            using (IDbConnection connection = new SqlConnection(Helper.ConnectionValue("CreeMesRtp")))
            {
                var parameters = new DynamicParameters(paramDictionary);
                var SQL = @"
                        Select
                        CMMSSB.MP2_SITEINFO.ITEMNUM,
                        CMMSSB.MP2_SITEINFO.REORDERPOINT,
                        CMMSSB.MP2_SITEINFO.REORDERQTY,
                        CMMSSB.MP2_SITEINFO.LASTDATECOUNTED,
                        CMMSSB.MP2_SITEINFO.LASTDATERCVD,
                        CMMSSB.MP2_SITEINFO.LASTACTIVITY
                        From
                        CMMSSB.MP2_SITEINFO
                        ";

                var Output = connection.Query<MP2SiteTableDefinition>(SQL, parameters).ToList();
                op.Complete();
                return Output;
            }
        }
    }
}

标签: c#list

解决方案


更新:我检查了下面的代码

但是为什么你改变了完整的描述?

  public class Site
{
    public int PartID { get; set; }
    public string Location { get; set; }

}

public class Part
{
    public int PartID { get; set; }
    public string Description { get; set; }
    public List<Site> Location { get; set; }
}
var siteList = new List<Site>() {
            new Site{PartID = 1, Location = "Dhaka"},
            new Site{PartID = 2, Location = "Rajshahi"},
            new Site{PartID = 3, Location = "Khulna"},
            new Site{PartID = 2, Location = "Barisal"},
            new Site{PartID = 1, Location = "Narsingdi"},
        };
        var dictionary = siteList.GroupBy(s => s.PartID)
              .ToDictionary(s => s.Key,
                 s => s.ToList());

        var partList = new List<Part> (){
            new Part{ PartID  = 3, Description = "A", Location = null},
            new Part{ PartID  = 2, Description = "C", Location = null},
            new Part{ PartID  = 1, Description = "B", Location = null},
        };


        foreach(var part in partList)
        {
            part.Location = dictionary[part.PartID];
        }

推荐阅读