将数组拆分到另一个数组

时间:2012-11-01 07:37:02

标签: c# sql

我有一个包含

的列表
  1. Price
  2. Code
  3. 我已设法将列表拆分为数组,但我想进一步拆分数组,以便我可以单独获取PriceCode并将Price排序为升序。排序时,我需要将Code排在一起,因为Price适用于特定的Code

    所以它会是这样的: 原始清单:

    1588,8DNY;1488,ACNY;1288,7DPE;1888,8HUC;1488,8WNH;
    

    分割到arrPrice后:

    [1588,8DNY],[1488,ACNY],[1288,7DPE],[1888,8HUC],[1488,8WNH]
    

    第二次分裂2ndarrPrice:

    [1588],[1488],[1288],[1888],[1488]
    

    第二次分裂2ndarrCode:

    [8DNY],[ACNY],[7DPE],[8HUC],[8WNH]
    

    按升序排序价格:

    [1288],[1488],[1488],[1588],[1888]
    

    代码将相应地排序:

    [7DPE],[ACNY],[8HUC],[8WNH]
    

    我在第一次分裂后陷入困境。

      if (lblprices.Text != "")
        {
            arrprice = lblprices.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    
            Array.Sort(arrprice);
    
            for (i = 0; i < arrprice.Length; i++)
            {
                arr2 = arrprice[i].Split(',');
                SQL2 = "SELECT DISTINCT [TP].[TCode], ";
                SQL2 += "[TP].[TName], ";
                SQL2 += "[TP].[TName_CN],  ";
                SQL2 += "[TP].[TourType], ";
                SQL2 += "[TP].[LastUpdateDate], ";
                SQL2 += "[TP].[ValidityFrom],  ";
                SQL2 += "[TP].[ValidityTo], ";
                SQL2 += "[CL].[CountryCode], ";
                SQL2 += "[CL].[CityName] ";
    
                SQL2 += "FROM [CL], [TP], [TourItinerary],[TourHotel] ";
    
                SQL2 += "WHERE [TP].[Activation] = 1  ";
                SQL2 += "AND [TP].[TCode] = '" + arr2[1] + "' ";
                SQL2 += "ORDER BY [TP].[LastUpdateDate] DESC ";
    
                objConnTour.Open();
    
                SqlCommand command = new SqlCommand(SQL2, objConnTour);
                SqlDataReader dataReader = command.ExecuteReader();
    
                if (dataReader.Read())
                {
                    html += "<tr><td class=\"border\">" + dataReader["TCode"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["TName"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["TName_CN"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["TType"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["LastUpdateDate"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["ValidityFrom"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["ValidityTo"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["CountryCode"] + "</td>";
                    html += "<td class=\"border\">" + dataReader["CityName"] + "</td>";
                    html += "<td class=\"border\">from&nbsp;<span class=\"price-red\">S$<b>" + arr2[0] + "</b></span><td/></tr>";
                }
    
                dataReader.Close();
    
                objConnTour.Close();
            }
        }
        return html;
    

    上述代码无法按升序对价格进行排序。正如您在arr2拆分中看到的那样,我需要CodePrice,因为我将根据Code从数据库中检索数据。

    -------- ---------编辑

    好的问题是,如果价格在1000-1999之间,它就会完美排序。 但如果我的价格低于1000,则不会按升序排序

    示例是使用@jekcom

    提供的代码的结果截图

    这是我没有拆分List并从数据库中检索它们的原始

    Original, Unsorted

    这是使用@ jekcom代码

    的Sorted

    enter image description here

    请注意价格未按升序排序。

4 个答案:

答案 0 :(得分:0)

您可以使用List KeyValuePair并使用LINQ对它们进行排序。

IList<KeyValuePair<string, decimal>> items = new List<KeyValuePair<string, decimal>>();
items.Add(new KeyValuePair<string, decimal>("8DTY", 1589));
items.Add(new KeyValuePair<string, decimal>("1DNY", 1688));
items.Add(new KeyValuePair<string, decimal>("8DNY", 1888));
items.Add(new KeyValuePair<string, decimal>("8DNY", 1588));
items.Add(new KeyValuePair<string, decimal>("8ENY", 1589));

IOrderedEnumerable<KeyValuePair<string,decimal>> result =items.OrderBy(item => item.Value);

foreach (var keyValuePair in result)
{
    MessageBox.Show(keyValuePair.Key + " " + keyValuePair.Value);
}

答案 1 :(得分:0)

这样做的一种方法:

定义您的自定义竞争者以对数组进行排序 然后将其传递给排序方法

比较器

public class ProductComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        string[] _x = x.Split(',');
        string[] _y = y.Split(',');
        double priceY = double.Parse(_y[0]);
        double priceX = double.Parse(_x[0]);

        return priceX.CompareTo(priceY);
    }
}

您的代码

if (lblprices.Text != "")
    {
        arrprice = lblprices.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

        Array.Sort(arrprice, new ProductComparer());


        for (i = 0; i < arrprice.Length; i++)
        {
            arr2 = arrprice[i].Split(',');
            SQL2 = "SELECT DISTINCT [TP].[TCode], ";
            SQL2 += "[TP].[TName], ";
            SQL2 += "[TP].[TName_CN],  ";
            SQL2 += "[TP].[TourType], ";
            SQL2 += "[TP].[LastUpdateDate], ";
            SQL2 += "[TP].[ValidityFrom],  ";
            SQL2 += "[TP].[ValidityTo], ";
            SQL2 += "[CL].[CountryCode], ";
            SQL2 += "[CL].[CityName] ";

            SQL2 += "FROM [CL], [TP], [TourItinerary],[TourHotel] ";

            SQL2 += "WHERE [TP].[Activation] = 1  ";
            SQL2 += "AND [TP].[TCode] = '" + arr2[1] + "' ";
            SQL2 += "ORDER BY [TP].[LastUpdateDate] DESC ";

            objConnTour.Open();

            SqlCommand command = new SqlCommand(SQL2, objConnTour);
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.Read())
            {
                html += "<tr><td class=\"border\">" + dataReader["TCode"] + "</td>";
                html += "<td class=\"border\">" + dataReader["TName"] + "</td>";
                html += "<td class=\"border\">" + dataReader["TName_CN"] + "</td>";
                html += "<td class=\"border\">" + dataReader["TType"] + "</td>";
                html += "<td class=\"border\">" + dataReader["LastUpdateDate"] + "</td>";
                html += "<td class=\"border\">" + dataReader["ValidityFrom"] + "</td>";
                html += "<td class=\"border\">" + dataReader["ValidityTo"] + "</td>";
                html += "<td class=\"border\">" + dataReader["CountryCode"] + "</td>";
                html += "<td class=\"border\">" + dataReader["CityName"] + "</td>";
                html += "<td class=\"border\">from&nbsp;<span class=\"price-red\">S$<b>" + arr2[0] + "</b></span><td/></tr>";
            }

            dataReader.Close();

            objConnTour.Close();
        }
    }
    return html;

数组结果

1288,7DPE
1488,ACNY
1488,8WNH
1588,8DNY
1888,8HUC

答案 2 :(得分:0)

以下是使用Linq的方法

string s = "1588,8DNY;1488,ACNY;1288,7DPE;1888,8HUC;1488,8WNH";

var OrderedResult= s.Split(';').ToList<string>()
                    .Select(item => item.Split(','))
                    .Select(item => new { price = Int32.Parse(item[0]), code = item[1] })
                    .OrderBy(item => item.price);

foreach(var item in OrderedResult)
{
   var sql = "select ..... where price=" + item.price + " and code=" + item.code;
   execute(sql);
}

答案 3 :(得分:0)

您发布的代码不起作用,因为您没有根据数组的Price部分对数组进行排序。如果您有Linq

if (lblprices.Text != "")
{
    arrprice = lblprices.Text.Split(new char[] { ';' }, 
                                    StringSplitOptions.RemoveEmptyEntries);
    var lst = arrprice.OrderBy(x => int.Parse(x.Split(',')[0])).ToList();

    for (i = 0; i < lst.Count; i++)
    {
        arr2 = lst[i].Split(',');

        //rest of your code ----------------
    }

这应该可行,在sql术语中应该给出

ORDER BY PRICE ASC, [TP].[LastUpdateDate] DESC