0

我有两个类列表——“交付”和“取件”——我正在尝试制作第三个列表,将交付和取件添加在一起,即显示所有交付和取件而不是一个或另一个的列表。

在搜索了一下之后,我尝试了这个:

public List<String> listVisits()
    {
        List<String> listVisits = new List<string>();
        listVisits.AddRange(listDeliveries);
        listVisits.AddRange(listPickups);
    }

但我只是收到错误说:

The best overloaded method match for 'System.Collections.Generic.List<string>.AddRange(System.Collections.Generic.IEnumerable<string>)' has found some invalid arguments

还有这个:

Argument 1: cannot convert from 'method group' to 'System.Collections.Generic.IEnumerable<string>'

这是什么意思,我该如何解决?

谢谢。

编辑:当前用户通过窗体输入数据。Deliveries 有两个 String 和一个 TimeSpan,Pickups 继承自 Deliveries,但还有两个 String。

    public List<String> listDeliveries()
    {
        List<String> listDeliveries = new List<string>();
        foreach (deliveries Deliv in delivery)
        {
            String DelivAsString = Deliv.DeliveryString();
            listDeliveries.Add(DelivAsString);
        }
        return listDeliveries;
    }

    public List<String> listPickups()
    {
        List<String> listPickups = new List<string>();
        foreach (pickups Pickup in pickup)
        {
            String PickupAsString = Pickup.PickupString();
            listPickups.Add(PickupAsString);
        }
        return listPickups;
    }
4

2 回答 2

6

错误消息表明listDeliveriesandlistPickups方法,而不是变量。所以你需要调用这些方法。同时,我强烈建议您重命名方法以遵循 .NET 命名约定。所以你可能有:

public List<String> ListVisits()
{
    List<String> visits = new List<string>();
    visits.AddRange(ListDeiveries());
    visits.AddRange(ListPickups());
    return visits;
}

或使用 LINQ:

public List<String> ListVisits()
{
    return ListDeliveries().Concat(ListPickups()).ToList();
}
于 2012-12-01T23:59:23.390 回答
0

我建议重新启动这样的事情:

public List<string> GetDeliveryStrings()
{
    //assuming "deliveries" is your collection of deliveries 
    //otherwise your coding standards are not, well, standard
    //additionally, per Tim Schmelter's suggestion, take a look at 
    //overriding the .ToString() method
    return deliveries.Select(d => d.DeliveryString()).ToList()
}

public List<String> GetPickupStrings()
{
    return pickups.Select(p => p.PickupString()).ToList();
}

//lifted from Jon Skeet's answer
public List<String> GetVisitStrings()
{
   return GetDeliveryStrings().Concat(GetPickupStrings()).ToList();
}
于 2012-12-02T00:10:38.790 回答