在 WCF 中使用字典

using dictionary with WCF

我正在尝试 return 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 。我不确定还有什么我必须传递给 return 字典有什么帮助吗?谢谢 M

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

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; }
}

一般来说,如果您要通过 WCF return 数据。数据class定义。 (在这种情况下 LanguageList )应该用 [DataContract] 装饰 以及具有 DataMember

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

试一试。