-5
我在C#中有4個項目的列表。此列表用於在Web服務中發送響應,並且我需要項目的特定順序,但是我遇到問題,因爲出於某種原因,列表在我填充時會更改順序。如何更改訂單屬性在C#中序列化#
首先,這是班級列表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Mvm.SATWeb.Domain
{
[Serializable, DataContract]
public class puntoDeAtencion
{
public puntoDeAtencion()
{
}
[DataMember]
public string codigoPuntoAtencion { get; set; }
[DataMember]
public decimal montoIngreso { get; set; }
[DataMember]
public decimal montoEgreso { get; set; }
[DataMember]
public decimal ingresoNeto { get; set; }
}
}
我使用SQL服務器查詢與數據集
List<puntoDeAtencion> valores = new List<puntoDeAtencion>();
DataSet ds;
Database baseDatos = DatabaseFactory.CreateDatabase();
DbCommand comandoConsulta = baseDatos.GetStoredProcCommand("USP_RiesgoLiqui");
comandoConsulta.CommandTimeout = 600000;
baseDatos.AddInParameter(comandoConsulta, "@pvstrIdAgencia", DbType.String, "-1");
baseDatos.AddInParameter(comandoConsulta, "@pvstrFechaInicial", DbType.String, FechaIni);
baseDatos.AddInParameter(comandoConsulta, "@pvstrFechaFinal", DbType.String, FechaFin);
comandoConsulta.CommandTimeout = 1000000;
// baseDatos.ExecuteDataSet();
ds = baseDatos.ExecuteDataSet(comandoConsulta);
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count ; i++)
{
// valores.Add(s)
//valores.Add (new punptoDeAtencion(){}
valores.Add(new puntoDeAtencion() { codigoPuntoAtencion = Convert.ToString(ds.Tables[0].Rows[i]["Agencia"]), montoIngreso = Convert.ToDecimal(ds.Tables[0].Rows[i]["INGRESONETO"]), montoEgreso = Convert.ToDecimal(ds.Tables[0].Rows[i]["MONTOEGRESO"]), ingresoNeto = Convert.ToDecimal(0.00) });
// var list1 = (from p in ds.Tables[0].Rows[i] select p).ToList();
}
}
return valores.ToList<puntoDeAtencion>();
這是響應填充列表(使用SOAP UI,但當我調試顯示在響應對象中相同的值)
<b:listaPuntosDeAtencion>
<b:puntoDeAtencion>
<b:codigoPuntoAtencion>001</b:codigoPuntoAtencion>
<b:ingresoNeto>0</b:ingresoNeto>
<b:montoEgreso>53266155.0000</b:montoEgreso>
<b:montoIngreso>138285187.0000</b:montoIngreso>
</b:puntoDeAtencion>
這就是它應該如何
<listaPuntosDeAtencion>
<puntoDeAtencion>
<codigoPuntoAtencion>00654</codigoPuntoAtencion>
<montoIngreso>79000.0</montoIngreso>
<montoEgreso>30000.0</montoEgreso>
<ingresoNeto>0.0</ingresoNeto>
</puntoDeAtencion>
我想訂購清單或迴應,我不知道LINQ是否適用於這種情況。
這不是一個列表順序 - 你總是隻顯示一個對象。您似乎更喜歡序列化對象中指定的屬性順序。如果是這樣,爲什麼這很重要? –
因爲響應將被用於其他服務需要這個特定的順序。 – Jmperez
那麼,使用'[DataMember]'的'Order'屬性。 –