在另一个列表中添加列表项
好的,我有一个类似于以下内容的类...
public class Order
{
private Guid id;
[DataMember]
public Guid ID
{
get { return id; }
set { id = value; }
}
private List<Items> orderItems;
[DataMember]
public List<Items> OrderItems
{
get { return orderItems; }
set { orderItems= value; }
}
}
public class Items
{
private string itemName;
[DataMember]
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
}
当我在代码中引用时,我有一个方法,该方法接受“订单”列表作为参数。
ACME.Order newOrder = new ACME.Order();
ACME.Items newItems = new ACME.Items();
newOrder.ID = xxx
newItems.ItemName = xxx
SendOrderWithItemsFunction(newOrder)
上面的工作正常,但是我没有为我的项目添加功能,因此我可以执行类似以下的操作
newOrder.Items.Add(newItem);
,但
newOrder.Items = newItems
不会工作,因为它说它不能隐式地将 newOrder.Items 转换为 newItems[]。
缺少什么?
Ok I have a class similar to the following...
public class Order
{
private Guid id;
[DataMember]
public Guid ID
{
get { return id; }
set { id = value; }
}
private List<Items> orderItems;
[DataMember]
public List<Items> OrderItems
{
get { return orderItems; }
set { orderItems= value; }
}
}
public class Items
{
private string itemName;
[DataMember]
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
}
When I reference in my code I have a method that takes in an "Order" list as the parameter.
ACME.Order newOrder = new ACME.Order();
ACME.Items newItems = new ACME.Items();
newOrder.ID = xxx
newItems.ItemName = xxx
SendOrderWithItemsFunction(newOrder)
The above works fine however I don't have an add function for my items so that I can do something like the following
newOrder.Items.Add(newItem);
and
newOrder.Items = newItems
will not work because it says that it can not implicitly convert newOrder.Items to newItems[].
What am Missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想我可能遗漏了一些东西,但是根据您在帖子中的内容,
newOrder.OrderItems.Add(newItem)
应该可以正常工作。只是其他一些挑剔的事情:
如果“Items”类只是一个单个 Item,那么它的复数形式是很奇怪的。这可能就是将单个项目分配给 List 属性看起来“可以”的原因。
您可能已将其从帖子中删除,但 WCF 序列化的每个类都必须标记为“DataContract”,而不仅仅是该类的成员。
当初始化这样的对象时,我认为使用 类型初始化器< 会更清晰/a>:
I think I might be missing something, but
newOrder.OrderItems.Add(newItem)
should work just fine, according to waht you have in your post.Just some other nitpick things:
The pluralization of the "Items" class is wierd, if it is only a single Item. This is probably the reason that it looked "ok" to assign a single item to a List property.
You may have cut it out of your post, but every class that is being serialized by WCF must be marked as a "DataContract", not just the members of the class.
When initializing objects like this, I think it makes it a lot cleaer to use Type Initializers:
您所拥有的是
Order.OrderItems
属性中的添加函数:您甚至可以将整个项目列表添加到您的 OrderItems 中:
What you do have is an add function in your
Order.OrderItems
property:you can even add a whole list of items to your OrderItems:
你应该能够这样做:
如果你的 newItems[] 是一个数组,你需要这样做:
You should be able to do:
If your newItems[] is an array, you need to do this:
您已将
newItems
声明为ACME.Items
类型,但Order
类的OrderItems
属性是 <代码>列表<项目>。这些类型不能直接从一种类型分配给另一种类型。因此,newOrder.OrderItems = newItems
的赋值就像试图说List; = 项目
。根据您上面概述的课程,这是不可能的。相反,您需要添加到列表中。You have declared
newItems
as anACME.Items
type, but theOrderItems
property of yourOrder
class is aList<Items>
. Those types are not assignable from one to the other directly. So, an assignment ofnewOrder.OrderItems = newItems
is like trying to sayList<Items> = Items
. That isn't possible based on the classes you outlined above. Instead, you will need to add to the list.当列表中有一个列表,并且缺少 Add() 方法时,解决方法是创建一个新列表,添加项目,然后将内部列表设置为新列表。而不是:
..使用..
When you have a list within a list, and the Add() method is missing, a workaround is to make a new list, add the items, then set the inner list to the new list. Instead of:
..use..