开发者

Can't add value to List in C#

开发者 https://www.devze.com 2022-12-10 12:15 出处:网络
I\'d like to add a value to a struct if (!existISDNteilnehmer(split)) { isdnObjs.Add(new ISDN() { name = split, number = \"\",

I'd like to add a value to a struct

if (!existISDNteilnehmer(split))
{
    isdnObjs.Add(new ISDN() { name = split, number = "", 
                        channels = new List<string>()});
}                
ISDN? actua开发者_如何转开发lISDN = 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;

           }
     }

Everything is read out correctly and the correct number is stored in part_nummer.

Now I want to add the number to the list with actualISDN.Value.number = part_nummer but I get an error that says it cannot be implicitly converted.

Where am I going wrong?


You're trying to assign a string to an IEnumerable it looks like.

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

actualISDN.Value.number = part_nummer;

You should be doing this instead:

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

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


Don't use var and the problem will become clear.

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


When you write this:

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

part_nummer contains an IEnumerable<T>. You'd better write something like that:

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

actualISDN.Value.number = part_nummer;


Can't read your code it doesn't render Well on a small screen but taken from the comments try wrapping the part_number linq in (linq goes here).Single()

0

精彩评论

暂无评论...
验证码 换一张
取 消