Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中调用web服务存储到列表中#_C#_Asp.net_List_Web Services - Fatal编程技术网

C# 如何在C中调用web服务存储到列表中#

C# 如何在C中调用web服务存储到列表中#,c#,asp.net,list,web-services,C#,Asp.net,List,Web Services,我必须使用服务绑定下拉列表,为此,我从.cs调用服务,并使用AJAX调用coll此方法并绑定下拉列表。 但我无法将其添加到服务中。获取错误无法将'Seystem.Collections.Generic.List'隐式转换为'Seystem.Collections.Generic.List 我需要做的 public Almarai.GiveAway.GetInitiatorList.INITIATORS_LIST GetInitiatorsListByWorkflow(string userId,

我必须使用服务绑定下拉列表,为此,我从.cs调用服务,并使用AJAX调用coll此方法并绑定下拉列表。 但我无法将其添加到服务中。获取错误
无法将'Seystem.Collections.Generic.List'隐式转换为'Seystem.Collections.Generic.List
我需要做的

public Almarai.GiveAway.GetInitiatorList.INITIATORS_LIST GetInitiatorsListByWorkflow(string userId, string WorkflowTypeCode) {
            return base.Channel.GetInitiatorsListByWorkflow(userId, WorkflowTypeCode);
        }

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ALM_COUNTRY_M", Namespace="http://schemas.datacontract.org/2004/07/Almarai.Web.Services.DataEntities")]
[System.SerializableAttribute()]

public partial class ALM_COUNTRY_M : object, System.Runtime.Serialization.IExtensibleDataObject, 
    System.ComponentModel.INotifyPropertyChanged {
    
    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private bool COUNTRY_ACTIVEField;
    
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private int COUNTRY_M_IDField;
    
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string CountryCodeField;
    
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string CountryNameField;
[System.Runtime.Serialization.DataMemberAttribute()]
public string CountryCode {
    get {
        return this.CountryCodeField;
    }
    set {
        if ((object.ReferenceEquals(this.CountryCodeField, value) != true)) {
            this.CountryCodeField = value;
            this.RaisePropertyChanged("CountryCode");
        }
    }
}

[System.Runtime.Serialization.DataMemberAttribute()]
public string CountryName {
    get {
        return this.CountryNameField;
    }
    set {
        if ((object.ReferenceEquals(this.CountryNameField, value) != true)) {
            this.CountryNameField = value;
            this.RaisePropertyChanged("CountryName");
        }
    }
}

[System.Runtime.Serialization.DataMemberAttribute()]
public string DivisionCode {
    get {
        return this.DivisionCodeField;
    }
    set {
        if ((object.ReferenceEquals(this.DivisionCodeField, value) != true)) {
            this.DivisionCodeField = value;
            this.RaisePropertyChanged("DivisionCode");
        }
    }
}

[System.Runtime.Serialization.DataMemberAttribute()]
public int DivisionId {
    get {
        return this.DivisionIdField;
    }
    set {
        if ((this.DivisionIdField.Equals(value) != true)) {
            this.DivisionIdField = value;
            this.RaisePropertyChanged("DivisionId");
        }
    }
}

[System.Runtime.Serialization.DataMemberAttribute()]
public string DivisionName {
    get {
        return this.DivisionNameField;
    }
    set {
        if ((object.ReferenceEquals(this.DivisionNameField, value) != true)) {
            this.DivisionNameField = value;
            this.RaisePropertyChanged("DivisionName");
        }
    }
}
方法:

public static List<CountryList> GetCountriesName(string UserID)
{
    GetInitiatorList.MasterDataServiceClient oClient = new GetInitiatorList.MasterDataServiceClient();
    string userid = "approver01";
    string work = "4";
    oClient.GetInitiatorsListByWorkflow(userid, work).ToString();

    List<CountryList> lst = new List<CountryList>();
    lst.Add(oClient.GetInitiatorsListByWorkflow(userid, work));
    return lst;
}
我已经添加了启动器类,我正在寻找这些文件绑定到下拉列表,任何人都可以帮助我这样做

请看屏幕截图


错误很明显,您不能用
列表设置
列表

您可以更改函数声明:

public static List<string> GetCountriesName(string UserID)
假设您想要返回一个
列表
,您可能需要选择要返回的
国家列表
的哪个属性,然后将其添加到列表中:

public static List<CountryList> GetCountriesName(string UserID)
{
    GetInitiatorList.MasterDataServiceClient oClient = new GetInitiatorList.MasterDataServiceClient();
    string userid = "approver01";
    string work = "4";

    List<string> lst = new List<string>();
    List<CountryList> countries = oClient.GetInitiatorsListByWorkflow(userid, work);

    foreach (Country c in countries)
    {
        lst.Add(c.Name);
    }

    return lst;
}
公共静态列表GetCountriesName(字符串用户ID)
{
GetInitiatorList.MasterDataServiceClient oClient=新建GetInitiatorList.MasterDataServiceClient();
字符串userid=“approver01”;
字符串工作=“4”;
List lst=新列表();
List countries=oClient.getInitiatorListbyWorkflow(userid,work);
foreach(国家/地区中的国家/地区c)
{
第一次添加(c.名称);
}
返回lst;
}

在澄清您评论中的退货类型后:
您正在尝试将类型为
INITIATORS\u LIST
的对象添加到类型为
CountryList
的列表中。要解决此问题,您必须添加类型为
CountryList
的对象:

var countryList = new CountryList
{
   ExtensionData = oClient.GetInitiatorsListByWorkflow(userid, work)
};
lst.Add(countryList);
而不是:

lst.Add(oClient.GetInitiatorsListByWorkflow(userid, work));
根据您的项目结构,您可能需要删除
CountryList
类中
ExtensionData
属性的
set
上的
internal
修饰符。如果这不是一个选项,您可以将构造函数添加到
CountryList
类中,该类将
INITIATORS\u LIST
作为参数:

public class CountryList
{
    public CountryList(INITIATORS_LIST data)
    {
        ExtensionData = data;
    }

    public INITIATORS_LIST ExtensionData { get; internal set; }
}

您可以使用
var lst=countries.Select(c=>c.Name.ToList()
@PanagiotisKanavos,如果它使用LINQ是的,但它在问题或标记中都没有说明任何关于LINQ的内容。但是同样的错误coming@AndrésMarotta LINQ只是一个名称空间,而不是一个数据访问库
Enumerable.ToList()
执行此循环在一行中所做的操作,而不赋予列表更大的作用域,也不允许它以部分构造的形式存在。@NitishKumarPatel如果更改返回类型,您将永远不会得到相同的错误。这就是问题代码中的问题。它试图退回错误的东西。你没有在问题本身中解释什么是正确的。名单?还是对象列表?唯一缺少的是
Name
属性,但由于OP不愿意提供更多信息,希望这个答案足够。@John我按照您的建议更新了我的代码,但仍然面临错误,请帮助me@croxy请看我得到的屏幕截图Error@croxy我无法解决此问题。请查找我的更新版本Code@NitishKumarPatel请避免发布错误截图或代码片段。始终将它们作为文本发布。这将使我们能够快速复制它们。第一个错误是因为您没有返回任何添加
returnlst结束时将修复此问题。对于第二个错误,您需要告诉我错误是怎么说的;这段对话已经结束。
lst.Add(oClient.GetInitiatorsListByWorkflow(userid, work));
public class CountryList
{
    public CountryList(INITIATORS_LIST data)
    {
        ExtensionData = data;
    }

    public INITIATORS_LIST ExtensionData { get; internal set; }
}