Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

asp.net - C# how union with object of type IEnumerable<KeyValuePair<Guid, string>>

In my ASP.NET MVC project I have a code like below that I have variable "CheckListItems" with type of "IEnumerable<KeyValuePair<Guid, string>>" that at the end I check a condition to add another item to this variable:

public class Person
{
    public Guid ID { get; set; }
    public string Caption { get; set; }
}

var chliList = new List<Person>()
{
    new Person{ID = Guid.NewGuid(), Caption = "Person1"}
};

var pointRelatedColumns = new List<KeyValuePair<Guid, string>> { };
    
pointRelatedColumns.Add(new KeyValuePair<Guid, string>(Guid.Parse("00000000-0000-0000-0000-000000000004"), "one"));
    
var CheckListItems = chliList
        .Select(x => new KeyValuePair<Guid, string>(x.ID, x.Caption))
        .Union(pointRelatedColumns)
        .Union(new List<KeyValuePair<Guid, string>> {
            new KeyValuePair<Guid, string>(Guid.Parse("00000000-0000-0000-0000-000000000003"), "two")
        });
    

And I try union below line with the condition that you can see here but it will not be added:

    if (pointValueAbsolute >= selectManMandatoryLevel)
    {
        var values = new[] {
            new KeyValuePair<Guid, string>(Guid.Parse("00000000-0000-0000-0000-000000000006"), "three")
        };
        CheckListItems.Union(values);
    }

How can I solve this problem? Any help will be appriciated!

question from:https://stackoverflow.com/questions/65670985/c-sharp-how-union-with-object-of-type-ienumerablekeyvaluepairguid-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
using System;
using System.Collections.Generic;
using System.Linq;

namespace StackOverflow
{
    public class Person
    {
        public Guid ID { get; set; }
        public string Caption { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var chliList = new List<Person>
            {
                new Person{ID = Guid.NewGuid(), Caption = "Person1"}
            };

            var pointRelatedColumns = new List<KeyValuePair<Guid, string>>();

            var checkListItems = chliList
                .Select(x => new KeyValuePair<Guid, string>(x.ID, x.Caption))
                .Union(pointRelatedColumns)
                .Union(new List<KeyValuePair<Guid, string>> {
                    new KeyValuePair<Guid, string>(Guid.Parse("00000000-0000-0000-0000-000000000003"), "two")
                })
                .ToList();

            if (true) //(pointValueAbsolute >= selectManMandatoryLevel)
            {
                var values = new List<KeyValuePair<Guid, string>>  
                {
                    new KeyValuePair<Guid, string>(Guid.Parse("00000000-0000-0000-0000-000000000006"), "three")
                };

                checkListItems.AddRange(values);
            }
        }
    }
}

Union returns reference to the new structure but don't change initial ones. Use AddRange instead for adding two lists of the same type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...