0

对 WCF 来说是全新的。真的可以使用一些帮助。

我有一个 (100) 个对象的列表,其中包含 4 个数据成员。DM1、DM2、DM3、DM4

我有一堆数据合同

DC1
 List<DC2>

DC2
 <DM1 Value=n> (n could be any number below 5, so there could be up to 5 DC2 inside the List in DC1)
 List<DC3>

DC3
 <DM2 value=n> (n could be any number below 10, so up to 10 DC3 inside the List in DC2)
 List<DC4>

DC4
 <DM3>
 <DM4>

需要遍历原始对象列表,并根据原始对象列表的数据成员中的不同值,使用各种嵌套数据合约创建一个数据合约(DC1)。

例如:

<DC1>
  <DC2>
    <DM1 value = "a">
    <DC3>
      <DM2 value = 1>
      <DC4>
        <DM3>
        <DM4>
      <DM2 value = 2>
      <DC4>
        <DM3>
        <DM4>
      <DC4>
        <DM3>
        <DM4>
    </D3>
  </DC2>
  <DC2>
    <DM1 value = "b">
    <DC3>
      <DM2 value = 1>
      <DC4>
        <DM3>
        <DM4>
      <DM2 value = 2>
      <DC4>
        <DM3>
        <DM4>
      <DC4>
        <DM3>
        <DM4>
    </D3>
  </DC2>
<DC1>

这样做的最佳方法是什么?

谢谢!

这是 DataContracts 以及使用 DC1 传递信息的服务合同:

[DataContract]
public class DC1
{
    [DataMember]
    public string string { get; set; }

    [DataMember]
    public List<DC2> LDC2{ get; set; }
}

[DataContract] public class DC2 { [DataMember] public string Type{ get; 放; }

    [DataMember]
    public List<DC3> DC3s{ get; set; }

[DataContract] public class DC3 { [DataMember] public decimal num { get; 放; }

    [DataMember]
    public List<DC4> DC4s{ get; set; }

[DataContract] public class DC4 { [DataMember] public int num2 { get; 放; }

    [DataMember]
    public decimal Val{get; set;}

服务契约:[ServiceContract(Namespace = Constants.Namespace)] public class Service {

    [OperationContract]
    DC1 GetMethod(int num);

数据集:

是以下对象的 LIST<>:

[Serializable]
public class Data
{
    public string Type { get; set; }
    public double Num { get; set; }
    public double Num2{ get; set; }
    public decimal Val{ get; set; }

}

此列表中可以有 100 个(或更多)这些对象。最多5种,Num最多10种,Num2最多5种。

感谢您的回复,并希望这可以澄清!

4

1 回答 1

1

可能有比让 LINQ 完成所有工作更有效的方法。但是,这似乎是一个关于如何将非规范化数据转换为规范化对象的问题,因此 GroupBy 作为一种易于编码的解决方案跳出来了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace WcfTestService
{
    [ServiceContract]
    public interface ISoapService
    {
        [OperationContract]
        DC1 GetDC1();
    }

    public class SoapService : ISoapService
    {
        public DC1 GetDC1()
        {
            // Example dataset
            var dataset = new List<Data>()
            {
                new Data() { Type = "a", Num = 1, Num2 = 1, Val = 41.00m },
                new Data() { Type = "a", Num = 2, Num2 = 1, Val = 42.00m },
                new Data() { Type = "a", Num = 2, Num2 = 2, Val = 43.00m },
                new Data() { Type = "b", Num = 1, Num2 = 1, Val = 44.00m },
                new Data() { Type = "b", Num = 2, Num2 = 1, Val = 45.00m },
                new Data() { Type = "b", Num = 2, Num2 = 2, Val = 46.00m },
            };

            // Process dataset into data contract objects
            return new DC1()
            {
                DC2s = dataset.GroupBy(x => x.Type).Select(typeGrouping => new DC2()
                {
                    DM1 = typeGrouping.Key,
                    DC3s = typeGrouping.GroupBy(x => x.Num).Select(numGrouping => new DC3()
                    {
                        DM2 = Convert.ToDecimal(numGrouping.Key),
                        DC4s = numGrouping.Select(x => new DC4()
                        {
                            DM3 = Convert.ToInt32(x.Num2),
                            DM4 = x.Val
                        }).OrderBy(x => x.DM3).ToList()
                    }).OrderBy(x => x.DM2).ToList()
                }).OrderBy(x => x.DM1).ToList()
            };
        }
    }

    [Serializable]
    public class Data
    {
        public string Type { get; set; }
        public double Num { get; set; }
        public double Num2 { get; set; }
        public decimal Val { get; set; }
    }

    [DataContract]
    public class DC1
    {
        [DataMember]
        public List<DC2> DC2s { get; set; }
    }

    [DataContract]
    public class DC2
    {
        [DataMember]
        public string DM1 { get; set; }
        [DataMember]
        public List<DC3> DC3s { get; set; }
    }

    [DataContract]
    public class DC3
    {
        [DataMember]
        public decimal DM2 { get; set; }
        [DataMember]
        public List<DC4> DC4s { get; set; }
    }

    [DataContract]
    public class DC4
    {
        [DataMember]
        public int DM3 { get; set; }
        [DataMember]
        public decimal DM4 { get; set; }
    }
}

GetDC1() 的输出应该非常接近您的示例输出,但都是 WCF'ified。

于 2013-02-14T02:52:43.700 回答