簡體   English   中英

比較兩個IEnumerable並根據比較結果刪除/插入

[英]compare two IEnumerable and delete/insert based on the comparison result

我必須將UI中的IEnumerable數據與SQL Server 2008中的IEnumerable數據進行比較,並且必須基於UI數據將UI中的新元素插入/更新到DB,並從DB中刪除元素。

我在數據庫中有Distribution_X_ListType表,如下所示:

DistributionID ListTypeID EmployeeNumber DepartmentID LocationID
1              2          84528          NULL         NULL 
1              3          NULL           8051         NULL
1              5          NULL           NULL         319

我在項目中有一個界面,如下所示:

public interface IDistributionList : IEntity
    {
        string Name { get; set; }
        bool ActiveFlag { get; set; }
        ...........................
        ...........................
        IEnumerable<IDistributionListType> ListTypes { get; set; }        
    }

另一個接口是:

public interface IDistributionListType
{
    int? DistributionID { get; set; }
    int ListTypeID { get; set; }
    int EmployeeNumber { get; set; }
    int DepartmentID { get; set; }
    int LocationID { get; set; }
}

在另一個項目中,我具有以下保存功能:

public int Save(IDistributionList distributionList)
        {
            SqlDataReader reader = null;            
            int rowsaffected = 0;
            try
            {
                sqlcommand = new SqlCommand("spGetDistributionListTypeByID", con);  //spGetDistributionListTypeByID - This SP returns all the members from Distribution_X_ListType table for the given distribution ID
                sqlcommand.CommandType = CommandType.StoredProcedure;
                sqlcommand.Parameters.AddWithValue("@Distribution_ID", distributionList.ID);

                reader = sqlcommand.ExecuteReader();

                while (reader.Read())
                {
                      ????Value Should be stored in an IDistributionListType variable,say IDistributionListTypeFromDB 
                }
                ????IDistributionListType from function parameter(Lets say from UI) should be compared with the above IDistributionListTypeFromDB data.
                ????Insert/Delete should be done in DB by comparing the data.
             }
         }

讓UI的IDistributionListType的值:

DistributionID ListTypeID EmployeeNumber DepartmentID LocationID
1              2          84528          NULL         NULL 
1              5          NULL           NULL         64   

讓來自數據庫的IDistributionListType的值:

DistributionID ListTypeID EmployeeNumber DepartmentID LocationID
1              2          84528          NULL         NULL 
1              3          NULL           8051         NULL
1              5          NULL           NULL         319

我需要使用UI中的數據更新數據庫。 我還有另外兩個SP:

spInsertUpdateDistributionListType - Insert/Update Distribution_X_ListType table  based on DistributionID and listtypeID
spDeleteDistributionListType - Delete Distribution_X_ListType table based on DistributionID and listtypeID

我不知道如何編碼? 區域(在.net代碼中提到)。 有人請幫忙。 提前致謝。

您應該只使用像Entity Framework這樣的O / R映射器,它將為您處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM