Union between two lists doesn't work

Kalyan A 265 Reputation points
2024-09-18T10:39:39.0766667+00:00

Union between two lists doesnt work getting error in

unionlist= groupsa.Union(groupsb);

@page "/SetTheory"
 
@using System.ComponentModel.DataAnnotations;  
 
  
<PageTitle>Set Theory</PageTitle>
 <div>
           SET A
  </div>
<ul>
    @foreach (var todo in todos)
    {
        <li>
            <input type="checkbox"  @bind="todo.IsDone" />
            <input  readonly @bind="todo.Title"  />
        </li>
    }
</ul
<input placeholder="Add a Number" @bind="newTodo" />
<button @onclick="AddTodo">Add Set A</button> <br>
<button @onclick="DeleteAll">Clear All Set A</button> <br>
<button @onclick="DeleteTodo">Delete Set A  </button>
<div>
           Ascending Order  
  </div>
 <ol>
    @foreach (var todo in  listsort)
    {
        <li>
            
            <input readonly @bind="todo.Title" />
           
        </li>
    }
</ol>
         <br />
         <ul>
    @foreach (var todob in todosb)
    {
        <li>
            <input type="checkbox"  @bind="todob.IsDone" />
            <input  readonly @bind="todob.Title"  />
        </li>
    }
</ul
<input placeholder="Add a Number Set B" @bind="newTodob" />
<button @onclick="AddTodoB">Add Set B</button> <br>
<button @onclick="DeleteAllB">Clear All Set B</button> <br>
<button @onclick="DeleteTodoB">Delete Set B  </button>
 
 <ol>
    @foreach (var todob in  listsortb)
    {
        <li>
            
            <input readonly @bind="todob.Title" />
           
        </li>
    }
</ol>
          <br />
 <button @onclick="Submit">Perform Calculation</button>
 
         <br />
          <div>
  <ul>
     
            @for (int i = 0; i < commonarr.Length; i++)
            {
                <li>
                    @if (display == "Y")
                    {
                        @commonarr[i]
                    }
                </li>
            }
 
</ul>
 </div> 
 
<div>
  
        @errmsg
    </div>
 
@code {
    public class TodoItem
    {
        public int Title { get; set; }
        public bool IsDone { get; set; }
    }
    public class TodoItemB
    {
        public int Title { get; set; }
        public bool IsDone { get; set; }
    }
    public class  Groups
    {
        public int Title { get; set; }
    }
    public class  GroupsA
    {
        public int Title { get; set; }
    }
    public class  GroupsB
    {
        public int Title { get; set; }
    }
    public class  UnionList
    {
        public int Title { get; set; }
    }
    public class  CommonList
    {
        public int Title { get; set; }
    }
    public class  Summary
    {
        public int Title { get; set; }
        public int cnts { get; set; }
    }
    private List<TodoItem> todos = new();
    private List<TodoItem> listsort = new();
    private List<TodoItemB> todosb = new();
    private List<TodoItemB> listsortb = new();
    private int newTodo;private int newTodob; int rancnta; int rancntb; int meansum; int maxnum;int minnum; int val1;int val2; int val0;
    private string errmsg;private string skipa;private string skipb; private int max;private int ran; private decimal calcmean; private decimal calcmedian; int median = 0; private int[] myNum = { 0  }   ; 
    private List<Groups> modegroups = new();private List<GroupsA>  groupsa = new();private List<GroupsB>  groupsb = new(); private List<UnionList>  unionlist = new();
    private List<Groups> tempgroups = new(); private string display = "N"; private int[] medarr; private int[] groupaarr; private int[] groupbarr; private int[] commonarr;
    private List<Summary> sumgroups = new();
    protected override void OnInitialized()
    {
    }
    private void DeleteAll()
    {
        int rem = todos.Count();
        if (rem > 0)
        {
            todos.Clear();
            listsort.Clear();
        }
        else
        { errmsg = "No records to Delete";}
    }
    private void DeleteTodo()
    {
        display = "N";  
        ran = 0;maxnum = 0;minnum= 0;
        rancnta = 0; rancntb = 0;meansum = 0; calcmean = 0;calcmedian = 0; val0 =0;val1 = 0;val2 = 0;
        int rem = todos.Count((s => s.IsDone == true));
        sumgroups.Clear();
        if (rem > 0)
        {
            var toRemove = todos.Where(todos => todos.IsDone == true).ToList();  
            foreach (var s in toRemove)
            {
                todos.Remove(s);
            }
        }
        else
        { errmsg = "Select a number to Delete";}
        listsort = todos.OrderBy(p => p.Title).ToList();
        groupsa =  listsort.Select( g => new GroupsA { Title = g.Title  } ).ToList();
    }
    private void AddTodo()
    {     display = "N";
        myNum[0] = 0;maxnum = 0;minnum= 0;  val0 =0; val1 = 0;val2 = 0;
        sumgroups.Clear();
        ran = 0;
        rancnta = 0; rancntb = 0;meansum = 0;calcmean = 0;calcmedian = 0;
        errmsg = "";
        skipa = "N";
        errmsg = "";
        if( (newTodo <  0) ||   (newTodo > 100)) 
        { skipa = "Y"; 
            errmsg = "Enter number between 0 and 100";}
        int cnt  = todos.Count();
        if (cnt>20)
        {   skipa = "Y"; 
            errmsg = "Cannot enter number more than 20 numbers";}
        if(skipa=="N")
        { 
            todos.Add(new TodoItem { Title = newTodo });
        }
        newTodo = 0;
        listsort = todos.OrderBy(p => p.Title).ToList();
        groupsa =  listsort.Select( g => new GroupsA { Title = g.Title  } ).ToList();
    }
    private void DeleteAllB()
    {
        int rem = todosb.Count();
        if (rem > 0)
        {
            todosb.Clear();
            listsortb.Clear();
        }
        else
        { errmsg = "No records to Delete";}
    }
    private void DeleteTodoB()
    {
        display = "N";  
        ran = 0;maxnum = 0;minnum= 0;
        rancnta = 0; rancntb = 0;meansum = 0; calcmean = 0;calcmedian = 0; val0 =0;val1 = 0;val2 = 0;
        int rem = todos.Count((s => s.IsDone == true));
        sumgroups.Clear();
        if (rem > 0)
        {
            var toRemove = todos.Where(todos => todos.IsDone == true).ToList();  
            foreach (var s in toRemove)
            {
                todos.Remove(s);
            }
        }
        else
        { errmsg = "Select a number to Delete";}
        listsortb = todosb.OrderBy(p => p.Title).ToList();
        groupsb =  listsortb.Select( g => new GroupsB { Title = g.Title  } ).ToList();
    }
    private void AddTodoB()
    {     display = "N";
        myNum[0] = 0;maxnum = 0;minnum= 0;  val0 =0; val1 = 0;val2 = 0;
        ran = 0;
        rancnta = 0;rancntb = 0;meansum = 0;calcmean = 0;calcmedian = 0;
        errmsg = "";
        skipb = "N";
        errmsg = "";
        if( (newTodob <  0) ||   (newTodob > 100)) 
        { skipb= "Y"; 
            errmsg = "Enter number between 0 and 100";}
        int cntb  = todosb.Count();
        if (cntb>20)
        {   skipb = "Y"; 
            errmsg = "Cannot enter number more than 20 numbers";}
        if(skipb=="N")
        { 
            todosb.Add(new TodoItemB { Title = newTodob });
        }
        newTodob = 0;
        listsortb = todosb.OrderBy(p => p.Title).ToList(); 
        groupsb =  listsortb.Select( g => new GroupsB { Title = g.Title  } ).ToList();
    }
    private void Submit()
    { display = "N";
        rancnta = todos.Count();
        rancntb = todosb.Count();
       
        if ((rancnta >0)||(rancntb >0))
        {
           var   tempgroupsa  =groupsa.Select(n =>  n.Title ).ToList();
        groupaarr = tempgroupsa.ToArray();
        var   tempgroupsb  =groupsb.Select(n =>  n.Title ).ToList();
        groupbarr = tempgroupsb.ToArray();
        var n =  groupaarr.Length;
    var m =  groupbarr.Length;
     
unionlist= groupsa.Union(groupsb);
    display = "Y";
         }}
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,576 Reputation points
    2024-09-20T17:15:33.9233333+00:00

    .Union() requires both sequences be the same type. in your code:

    private List<GroupsA>  groupsa = new();
    private List<GroupsB>  groupsb = new(); 
    
    ...
    unionlist= groupsa.Union(groupsb); 
    

    groupsa and groupsb are not sequences of the same type (GroupsA vs GroupsB). its not clear why you have so many class with the same properties. but the fix is map to common type (UnionList, the result type):

    unionlist = groupsa
        .Select(r => new UnionList {Title = r.Title})
        .Union(groupsb.Select(r => new UnionList {Title = r.Title}))
        .ToList(); 
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Kalyan A 265 Reputation points
    2024-09-19T09:23:02.81+00:00

    Union and Intersect doesnt work accomplished using for loop

    @page "/SetTheory"
     
    @using System.ComponentModel.DataAnnotations;  
     
      
    <PageTitle>Set Theory</PageTitle>
     <div>
               SET A
      </div>
    <ul>
        @foreach (var todo in todos)
        {
            <li>
                <input type="checkbox"  @bind="todo.IsDone" />
                <input  readonly @bind="todo.Title"  />
            </li>
        }
    </ul
    <input placeholder="Add a Number" @bind="newTodo" />
    <button @onclick="AddTodo">Add Set A</button> <br>
    <button @onclick="DeleteAll">Clear All Set A</button> <br>
    <button @onclick="DeleteTodo">Delete Set A  </button>
    <div>
               Ascending Order  
      </div>
     <ol>
        @foreach (var todo in  listsort)
        {
             <li style="display:inline"> 
                
                <input readonly @bind="todo.Title" />
               
            </li>
        }
    </ol>
             <br />
             <ul>
        @foreach (var todob in todosb)
        {
            <li >
                <input type="checkbox"  @bind="todob.IsDone" />
                <input  readonly @bind="todob.Title"  />
            </li>
        }
    </ul>
     
    <div>
               SET B
      </div>
    <input placeholder="Add a Number Set B" @bind="newTodob" />
    <button @onclick="AddTodoB">Add Set B</button> <br>
    <button @onclick="DeleteAllB">Clear All Set B</button> <br>
    <button @onclick="DeleteTodoB">Delete Set B  </button>
     
     <ol>
        @foreach (var todob in  listsortb)
        {
            <li style="display:inline">
                
                <input readonly @bind="todob.Title" />
               
            </li>
        }
    </ol>
              <br />
     <button @onclick="Submit">Perform Calculation</button>
     
             <br />
              <div> A - B </div>
              
      <ul>
         @if(aminusblist.Count()==0)
         {<span>Empty Result</span>}
                       
             @foreach (var aminusb in  aminusblist)
        {
            <li>
                
                @aminusb.Title
               
            </li>
        }
               
     
    </ul>
    <br/>
     <div> B - A </div>
    <br/>
    <ul>
             @if(bminusalist.Count()==0)
         {<span>Empty Result</span>}
                       
             @foreach (var bminusa in  bminusalist)
        {
            <li>
                
                @bminusa.Title
               
            </li>
        }
               
     
    </ul>
     <br/>
    <span> A Union B </span> 
     <br/>
        @if(unionlist.Count()==0)
         {<span>Empty Result</span>}
    <ul>
         
                       
             @foreach (var unionlst in   unionlist)
        {
            <li>
                
                @unionlst.Title
               
            </li>
        }
               
     
    </ul>
     <br/>
    <span> A Intersection B </span> 
     <br/>
        @if(interlist.Count()==0)
         {<span>Empty Result</span>}
    <ul>
         
                       
             @foreach (var interlst in   interlist)
        {
            <li>
                
                @interlst.Title
               
            </li>
        }
               
     
    </ul>
    <div>
      
            @errmsg
        </div>
     
    @code {
        public class TodoItem
        {
            public int Title { get; set; }
            public bool IsDone { get; set; }
        }
        public class TodoItemB
        {
            public int Title { get; set; }
            public bool IsDone { get; set; }
        }
        public class  Groups
        {
            public int Title { get; set; }
        }
        public class  GroupsA
        {
            public int Title { get; set; }
        }
        public class  GroupsB
        {
            public int Title { get; set; }
        }
        public class  UnionList
        {
            public int Title { get; set; }
        }
        public class  CommonList
        {
            public int Title { get; set; }
        }
        public class  AminusBList
        {
            public int Title { get; set; }
        }
        public class  Summary
        {
            public int Title { get; set; }
            public int cnts { get; set; }
        }
        private List<TodoItem> todos = new();
        private List<TodoItem> listsort = new();
        private List<TodoItemB> todosb = new();
        private List<TodoItemB> listsortb = new();
        private int newTodo;private int newTodob; int rancnta; int rancntb; int meansum; int maxnum;int minnum; int val1;int val2; int val0;
        private string errmsg;private string skipa;private string skipb; private int max;private int ran; private decimal calcmean; private decimal calcmedian; int median = 0; private int[] myNum = { 0  }   ; 
        private List<Groups> modegroups = new();private List<GroupsA>  groupsa = new();private List<GroupsB>  groupsb = new(); private List<CommonList>  unionlist = new(); private List<CommonList>  aminusblist = new();private List<CommonList>  bminusalist = new();private List<CommonList>  interlist  = new();
        private List<Groups> tempgroups = new(); private string display = "N"; private int[] medarr; private int[] groupaarr; private int[] groupbarr; private int[] commonarr; private int[] aminusb ={ 0  }   ; 
        private List<Summary> sumgroups = new();private string displayaminusb;
        private List<CommonList>  remlist  = new();
        protected override void OnInitialized()
        {
        }
        private void DeleteAll()
        {
            int rem = todos.Count();
            if (rem > 0)
            {
                todos.Clear();
                listsort.Clear();    aminusblist.Clear(); bminusalist.Clear();unionlist.Clear();interlist.Clear();
            }
            else
            { errmsg = "No records to Delete";}
        }
        private void DeleteTodo()
        {
            display = "N";  
            ran = 0;maxnum = 0;minnum= 0;    errmsg = "";
            rancnta = 0; rancntb = 0;meansum = 0; calcmean = 0;calcmedian = 0; val0 =0;val1 = 0;val2 = 0;
            int rem = todos.Count((s => s.IsDone == true));
            if (rem > 0)
            { aminusblist.Clear();bminusalist.Clear();unionlist.Clear();interlist.Clear();
                var toRemove = todos.Where(todos => todos.IsDone == true).ToList();  
                foreach (var s in toRemove)
                {
                    todos.Remove(s);
                }
            }
            else
            { errmsg = "Select a number to Delete";}
            listsort = todos.OrderBy(p => p.Title).ToList();
            groupsa =  listsort.Select( g => new GroupsA { Title = g.Title  } ).ToList();
        }
        private void AddTodo()
        {     display = "N";
            myNum[0] = 0;maxnum = 0;minnum= 0;  val0 =0; val1 = 0;val2 = 0;
            aminusblist.Clear();bminusalist.Clear();unionlist.Clear();interlist.Clear();
            ran = 0;
            rancnta = 0; rancntb = 0;meansum = 0;calcmean = 0;calcmedian = 0;
            errmsg = "";
            skipa = "N";
            errmsg = "";
            var resultx = todos.FirstOrDefault(c => c.Title == newTodo);
            if(resultx == null)
            { skipa = "N";}
            else  { skipa = "Y";errmsg = "Cannot Allow duplicates";}
            if( (newTodo <  0) ||   (newTodo > 100)) 
            { skipa = "Y"; 
                errmsg = "Enter number between 0 and 100";}
            int cnt  = todos.Count();
            if (cnt>20)
            {   skipa = "Y"; 
                errmsg = "Cannot enter number more than 20 numbers";}
            if(skipa=="N")
            { 
                todos.Add(new TodoItem { Title = newTodo });
            }
            newTodo = 0;
            listsort = todos.OrderBy(p => p.Title).ToList();
            groupsa =  listsort.Select( g => new GroupsA { Title = g.Title  } ).ToList();
        }
        private void DeleteAllB()
        {
            int rem = todosb.Count();
            errmsg = "";
            if (rem > 0)
            { aminusblist.Clear();bminusalist.Clear();unionlist.Clear();interlist.Clear();
                todosb.Clear();
                listsortb.Clear();
            }
            else
            { errmsg = "No records to Delete";}
        }
        private void DeleteTodoB()
        {
            display = "N";  
            ran = 0; 
            rancnta = 0; rancntb = 0; 
            int rem = todosb.Count((s => s.IsDone == true));
            aminusblist.Clear();bminusalist.Clear();unionlist.Clear();interlist.Clear();
            sumgroups.Clear();
            if (rem > 0)
            {
                var toRemoveb = todosb.Where(todos => todos.IsDone == true).ToList();  
                foreach (var s in toRemoveb)
                {
                    todosb.Remove(s);
                }
            }
            else
            { errmsg = "Select a number to Delete";}
            listsortb = todosb.OrderBy(p => p.Title).ToList();
            groupsb =  listsortb.Select( g => new GroupsB { Title = g.Title  } ).ToList();
        }
        private void AddTodoB()
        {     display = "N";
            myNum[0] = 0;maxnum = 0;minnum= 0;  val0 =0; val1 = 0;val2 = 0;
            aminusblist.Clear();bminusalist.Clear();unionlist.Clear();interlist.Clear();
            ran = 0;
            rancnta = 0;rancntb = 0;meansum = 0;calcmean = 0;calcmedian = 0;
            errmsg = "";
            skipb = "N";
            errmsg = "";
            var resultx = todosb.FirstOrDefault(c => c.Title == newTodob);
            if(resultx == null)
            { skipb = "N";}
            else  { skipb = "Y";errmsg = "Cannot Allow duplicates";}
            if( (newTodob <  0) ||   (newTodob > 100)) 
            { skipb= "Y"; 
                errmsg = "Enter number between 0 and 100";}
            int cntb  = todosb.Count();
            if (cntb>20)
            {   skipb = "Y"; 
                errmsg = "Cannot enter number more than 20 numbers";}
            if(skipb=="N")
            { 
                todosb.Add(new TodoItemB { Title = newTodob });
            }
            newTodob = 0;
            listsortb = todosb.OrderBy(p => p.Title).ToList(); 
            groupsb =  listsortb.Select( g => new GroupsB { Title = g.Title  } ).ToList();
        }
        private void Submit()
        { displayaminusb = "N"; aminusblist.Clear();bminusalist.Clear();unionlist.Clear();interlist.Clear();
            errmsg = "";
            rancnta = todos.Count();
            rancntb = todosb.Count();
            var tempgroupsa = groupsa.Select(n => n.Title).ToList();
            groupaarr = tempgroupsa.ToArray();
            var tempgroupsb = groupsb.Select(n => n.Title).ToList();
            groupbarr = tempgroupsb.ToArray();
            var n = groupaarr.Length;
            var m = groupbarr.Length;
            //A-B
            for (int i = 0; i < groupaarr.Length; i++)
            {
                var resulty = groupsb.FirstOrDefault(c => c.Title == groupaarr[i]);
                if (resulty == null)
                { displayaminusb = "Y";
                    aminusblist.Add(new CommonList { Title = groupaarr[i] });
                }
            }
            int aminuscount = aminusblist.Count();
            if(aminuscount==0)
            { errmsg = "Empty Result "; }
            if(n==0)
            {errmsg = "Empty Set A ";}
            //B-A
            for (int i = 0; i < groupbarr.Length; i++)
            {
                var resultz = groupsa.FirstOrDefault(c => c.Title == groupbarr[i]);
                if (resultz == null)
                { displayaminusb = "Y";
                    bminusalist.Add(new CommonList { Title = groupbarr[i] });
                }
            }
            int bminusacount = bminusalist.Count();
            // A Union B   
            for (int i = 0; i < groupaarr.Length; i++)
            {
                unionlist.Add(new CommonList { Title = groupaarr[i] });
            }
            for (int i = 0; i < groupbarr.Length; i++)
            {
                var resultx = unionlist.FirstOrDefault(c => c.Title == groupbarr[i]);
                if (resultx == null)
                {  
                    unionlist.Add(new CommonList { Title = groupbarr[i] });
                }
            }
            unionlist =  unionlist.OrderBy(p => p.Title).ToList(); 
            int unioncount = unionlist.Count();
            // A Intersection B   
            for (int i = 0; i < groupaarr.Length; i++)
            {
                var resultn = groupsb.FirstOrDefault(c => c.Title == groupaarr[i]);
                if (resultn != null)
                {
                    interlist.Add(new CommonList { Title = groupaarr[i] });
                }
            }
          
        }
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.