【问题标题】:using dictionary with WCF在 WCF 中使用字典
【发布时间】:2015-05-14 11:37:34
【问题描述】:

我正在尝试通过 WCF 返回字典

    public Dictionary<LanguageList> GetLanguageSettingList()
    {
        Repository exrepo = new Repository(this.ConnectionString);

        return exrepo.GetLanguageSettingList();
    }


public partial class LanguageList
{
    public string Language { get; set; }
    public Nullable<int> Id { get; set; }
}

错误为Error 1 Using the generic type 'System.Collections.Generic.Dictionary<TKey,TValue>' requires 2 type arguments c:\users\xxxxx\documents\visual studio 2013\projects\service\service\service.svc.cs 74 16 SaService 。我不确定我还需要传递什么才能返回字典有什么帮助吗?谢谢M

【问题讨论】:

  • 字典有两个类型参数而不是一个。另外,LanguageList 类型是否可序列化?
  • LanguageList 只是作为存储过程中的列表返回,但我的应用程序需要它作为字典
  • 我一直在读这篇文章,但运气不好stackoverflow.com/questions/1510185/…

标签: c# wcf


【解决方案1】:

您的语法在代码中有错误。 这才是你真正需要的:

public Dictionary<int, string> GetLanguageSettingList()
    {
        Repository exrepo = new Repository(this.ConnectionString);

        return exrepo.GetLanguageSettingList().Where(c=>c.Id!=null).ToDictionary(c=>c.Id.Value, c=>c.Language);
    }


public partial class LanguageList
{
    public string Language { get; set; }
    public Nullable<int> Id { get; set; }
}

【讨论】:

    【解决方案2】:

    一般来说,如果您要通过 WCF 返回数据。定义的数据类。 (在这种情况下 LanguageList )应该用 [DataContract] 装饰 以及拥有 DataMember 的成员

    [DataContract]
    public class X
    {
        [DataMember]
        int MyProp {get;set;}
    }
    

    试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多