无法在C#中为List添加值

时间:2009-11-02 14:24:33

标签: c# .net list struct

我想为struct

添加一个值
if (!existISDNteilnehmer(split))
{
    isdnObjs.Add(new ISDN() { name = split, number = "", 
                        channels = new List<string>()});
}                
ISDN? actualISDN = getISDN(split);

if (index < ISDN_teilnehmer.Count())
{
     var numbers = 
          from num in xISDN.XPathSelectElements("//member[name='number']")
               where num.IsAfter(xISDN) &&
                     num.IsBefore(ISDN_teilnehmer.ElementAt(index))
          select num;

     foreach (var nums in numbers)
     {
          if (nums.Element("name").Value == "number")
          {
               var nummer = nums.XPathSelectElements("value");
               var part_nummer = 
                   from n in nummer
                   select n.Value;
               //string temp = part_nummer;

               actualISDN.Value.number = part_nummer;

           }
     }

所有内容都被正确读出,正确的数字存储在part_nummer

现在我想使用actualISDN.Value.number = part_nummer将该号码添加到列表中,但我收到一条错误消息cannot be implicitly converted

我哪里错了?

4 个答案:

答案 0 :(得分:4)

不要使用var,问题就会变得清晰。

var part_nummer = from n in nummer
select n.Value;
//string temp = part_nummer;
actualISDN.Value.number = part_nummer;

答案 1 :(得分:4)

您正在尝试将字符串分配给它看起来像的IEnumerable。

var part_nummer = from n in nummer
    select n.Value;
//string temp = part_nummer;

actualISDN.Value.number = part_nummer;

你应该这样做:

var part_nummer = from n in nummer
    select n.Value;
//string temp = part_nummer;

actualISDN.Value.number = part_nummer.FirstOrDefault();

答案 2 :(得分:2)

当你这样写:

var part_nummer = from n in nummer
    select n.Value;

part_nummer包含IEnumerable<T>。你最好写点那样的东西:

var nummer = nums.XPathSelectElement("value"); // not XPathSelectElements !
var part_nummer = nummer.Value;

actualISDN.Value.number = part_nummer;

答案 3 :(得分:0)

无法读取你的代码它不能渲染好在一个小屏幕上,但从评论中取出尝试包装part_number linq in(linq到这里).Single()