Conversion from var to List

Kalyan A 265 Reputation points
2024-09-06T13:29:51.9233333+00:00

Here is a simple app to Add numbers from 0 to 100 , after which I like to perform some statistical operations like mean median mode.

I am getting the below error when trying to convert var to to list

( sumgroups = tempgroups;)

Severity Code Description Project File Line Suppression State

Error CS0266 Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<<anonymous type: int Title, int TitleCount>>' to 'System.Collections.Generic.List<MauiAppMCQs.Pages.Range.Summary>'. An explicit conversion exists (are you missing a cast?) MauiAppMCQs (net7.0-android) C:\Users\allam\source\repos\MauiAppMCQs\MauiAppMCQs\Pages\Range.razor 238 Active

@page "/Range"
 
@using System.ComponentModel.DataAnnotations;  
 
  
<PageTitle>Probability</PageTitle>
 
<ul>
    @foreach (var todo in todos)
    {
        <li>
            <input type="checkbox"  @bind="todo.IsDone" />
            <input  readonly @bind="todo.Title"  />
        </li>
    }
</ul
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo">Add</button> <br>
<button @onclick="DeleteTodo">Delete</button>
  <div>
   Ascending Order  
  </div>
 <ol>
    @foreach (var todo in listsort)
    {
        <li>
            
            <input readonly @bind="todo.Title" />
        </li>
    }
</ol>
<button @onclick="Submit">Perform Calc</button>
<div>
    Range:
  </div>
<div>
    @ran
    </div>
      <br />
    <div>
    Count:
  </div>
<div>
    @rancnt
    </div>
 
         <br />
    <div>
    Sum:
  </div>
<div>
    @meansum
    </div>
         <br />
    <div>
    Mean:
  </div>
<div>
    @calcmean
    </div>
         <br />
  <div>
    Mode(s)
  </div>  
  
  <div>
  <ul>
     
    @for  (int i = 0; i < myNum.Length; i++) 
    {
            <li>
                @if(display=="Y")
                { 
               @myNum[i] 
                }
             
            </li>
    }
       
</ul>
 </div>  
  <br />
  <div>
      Median
      </div>  
      <div>
    @calcmedian
    </div>
<div>
  
        @errmsg
    </div>
 
@code {
    public class TodoItem
    {
        public int Title { get; set; }
        public bool IsDone { get; set; }
    }
    public class  Groups
    {
        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 int newTodo; int rancnt; int meansum;
    private string errmsg;private string skip; 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<Groups> tempgroups = new(); private string display = "N"; private int[] medarr; 
    private List<Summary> sumgroups = new();
    protected override void OnInitialized()
    {
    }
    private void DeleteTodo()
    {
        display = "N";  
        ran = 0;
        rancnt = 0;meansum = 0; calcmean = 0;calcmedian = 0;
        int rem = todos.Count((s => s.IsDone == true));
        if (rem > 0)
        {
            var toRemove = todos.Where(todos => todos.IsDone == true).ToList();  
            foreach (var s in toRemove)
            {
                todos.Remove(s);
            }
        }
        listsort = todos.OrderBy(p => p.Title).ToList();
    }
    private void AddTodo()
    {     display = "N";
        myNum[0] = 0;
        ran = 0;
        rancnt = 0;meansum = 0;calcmean = 0;calcmedian = 0;
        errmsg = "";
        skip = "N";
        errmsg = "";
        if( (newTodo <  0) ||   (newTodo > 100)) 
        { skip = "Y"; 
            errmsg = "Enter number between 0 and 100";}
        int cnt  = todos.Count();
        if (cnt>20)
        {   skip = "Y"; 
            errmsg = "Cannot enter number more than 20 numbers";}
        if(skip=="N")
        { 
            todos.Add(new TodoItem { Title = newTodo });
        }
        newTodo = 0;
        listsort = todos.OrderBy(p => p.Title).ToList(); 
   
    }
    private void Submit()
    { display = "Y";
        meansum = todos.Sum(p => p.Title) ;
        rancnt = todos.Count();
        if (rancnt >0)
        { 
            ran =   todos.Max(p => p.Title)-todos.Min(p => p.Title) ;
            calcmean = Math.Round( Decimal.Divide(meansum ,  rancnt),2)  ;
            //
            var tempgroups =listsort.GroupBy(n => n.Title)
                             .Select(n => new
                             {
                                 Title = n.Key,
                                 TitleCount = n.Count()
                             })
                             .OrderBy(n => n.Title);
//The below line doesnt work
          sumgroups = tempgroups;
          //The above line doesnt work
            int groupmax = tempgroups.Max(p => p. TitleCount );
            var   modegroup  =tempgroups.Where(groups => groups.TitleCount == groupmax).Select(n =>  n.Title ).ToList();
            myNum =   modegroup.ToArray();
            var   medgroup  =listsort.Select(n =>  n.Title ).ToList();
            medarr = medgroup.ToArray();
            int size = medarr.Length;
            int mid = size/2;
            // int median = 0;
            if ((size % 2) != 0){
                calcmedian =  medarr[mid];
            }
            else {
                int val1 = medarr[mid - 1];
                int val2 = medarr[mid];
                int val3 = val1 + val2;
                calcmedian = Decimal.Divide(val3,2);
                calcmedian = Math.Round(calcmedian, 2);
            }
         }}
}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,584 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 117.6K Reputation points
    2024-09-06T14:04:13.0966667+00:00

    The line that does not work is not needed, therefore it can be removed.

    If it is needed, then:

    sumgroups = tempgroups.Select( g => new Summary { Title = g.Title, cnts = g.TitleCount } ).ToList( );
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.