2

我在 wcf 服务中有这样的方法

public string PostAllSurveList(List<Survey> surveyList)
        {
            var surveyDbList = ConstructDbServeyList(surveyList);
            foreach (var survey in surveyDbList)
            {
                _db.surveys.Add(survey);
            }
            _db.SaveChanges();
            return "Successfully Saved";
        }

现在我的问题是如何从客户端代码中调用此方法。这意味着首先我必须构建调查列表。我怎样才能构建这个列表。

string reply = client.PostAllSurveList(How can I construct this List?);

为了您的信息,我正在用 C# 编写客户端代码。

提前致谢。

4

4 回答 4

3
var list = new List<Survey>();    
string reply = client.PostAllSurveList(list);

而且你真的需要确保你知道如何拼写 Survey,因为你在 6 行代码中有 3 种不同的拼写。代码是书面语言,如果大声说出来有点相似,它就不起作用。

编辑: 确保在生成客户端时,选择“列表”作为任何集合的选项。看来您选择了数组,这意味着您的函数现在在客户端接受一个数组:

var list = new List<Survey>();    
string reply = client.PostAllSurveList(list.ToArray());
于 2013-05-23T09:04:39.267 回答
1

创建调查项目并将它们添加到列表中,将列表作为参数传递:

Survey survey1 = new Survey();

survey1.property1= value;
survey1.property2= value;

Survey survey2 = new Survey();

survey2.property1= value;
survey2.property2= value;

List<Survey> listSurvey = new List<Survey>();
listSurvey.add(survey1);
listSurvey.add(survey2);

string reply = client.PostAllSurveList(listSurvey);
于 2013-05-23T09:03:50.353 回答
1

像这样创建列表并提供:

var list = new List<Survey>();
string reply = client.PostAllSurveList(list);

编辑:更新对 ObservableCollection 的服务引用 在此处输入图像描述

于 2013-05-23T09:05:05.640 回答
0

尝试:

var list = new List<Survey>();
string reply = client.PostAllSurveList(list);

或使用Collection Initializer

string reply = client.PostAllSurveList(new List<Survey> { });

尽管list应该在业务逻辑的其他地方填充,除非ConstructDbServeyList直接在方法中操作变量。

.Add()您可以使用该方法添加到列表中。例如:

list.Add(new Survey() { Property = "Property"; });
于 2013-05-23T09:02:35.797 回答