locked
Add one list to master model specific list in c# RRS feed

  • Question

  • User-1965208702 posted

    Dear Support Team,

    I am using one master class where i have various list defined. I need to add each list data like below:

    Below is the master model :

     public class CostAnalysis
        {
            public string ID { get; set; }
            public string CancerType { get; set; }
            public decimal? Period1 { get; set; }
            public decimal? Period2 { get; set; }
            public decimal? Period3 { get; set; }
            public decimal? Period4 { get; set; }
            public decimal? Period5 { get; set; }
            public decimal? Benchmark { get; set; }
        }
        public class CostAnalysisSummary
        {
            public int id { get; set; }
            public List<CostAnalysis> Summary;
            public List<CostAnalysis> PartD { get; set; }
            public List<CostAnalysis> PartB { get; set; }
            public List<CostAnalysis> All { get; set; }
        }

    Then below is the method where i am trying to set one of the specific list value:

    public List<CostAnalysisSummary> GetCostAnalysisSummary(CostAnalysisParam rp)
            {
                List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
               
                List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>();
               
                var dataset = //Have some data from database
                if (dataset != null && dataset.Tables.Count > 0)
                    if (dataset.Tables[0].Rows.Count > 0)
                    {
                        
                        if (string.IsNullOrEmpty(rp.AppType))
                        {
                            _lstCostAnalysis = (from DataRow row in dataset.Tables[0].Rows
                                                    select new CostAnalysis
                                                    {
                                                       //list items binded here with business data.
                                                    }).ToList();
                            
                            _lstCostAnalysisSummary[0].Summary.Add(_lstCostAnalysis);
                            }
                        }
                        
                    }
    
                return _lstCostAnalysisSummary != null ? _lstCostAnalysisSummary : null;
            }

    As you can see above i am trying to add costAnalysis list item to master model specific list "Summary"

    But it is throwing compile time error:

    "cannot convert from 'system.collections.generic.list to model"

    Kindly help me in this.

    Friday, May 1, 2020 12:32 PM

Answers

  • User475983607 posted

    I'm not sure how to help you.  Your code sample is incomplete and the design is questionable at best. 

    From the looks of you code, I think you'll be interested in learning generics and basic class constructions

    public List<CostAnalysisSummary> GetCostAnalysisSummary(CostAnalysisParam rp)
    {
        List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
    
        List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>()
        {
            new CostAnalysisSummary()
            {
                    Summary = new List<CostAnalysis>()
            }
        };
    
        var dataset = //Have some data from database
    
        if (dataset != null && dataset.Tables.Count > 0)
        {
            if (dataset.Tables[0].Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(rp.AppType))
                {
                    _lstCostAnalysis = (from DataRow row in dataset.Tables[0].Rows
                                        select new CostAnalysis
                                        {
                                            //list items binded here with business data.
                                        }).ToList();
    
                    _lstCostAnalysisSummary[0].Summary.AddRange(_lstCostAnalysis);
                }
            }
        }
    
        return _lstCostAnalysisSummary != null ? _lstCostAnalysisSummary : null;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 1, 2020 3:40 PM

All replies

  • User475983607 posted

    Try using AddRange; https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.addrange?view=netcore-3.1

    _lstCostAnalysisSummary[0].Summary.AddRange(_lstCostAnalysis);

    Friday, May 1, 2020 1:20 PM
  • User-1965208702 posted

    Hi Sir,

    I tried the suggested code and got the error like ""Index was out of range must be non negative and less than the size of collections.

    Please suggest.

    Friday, May 1, 2020 1:51 PM
  • User475983607 posted

    Hi Sir,

    I tried the suggested code and got the error like ""Index was out of range must be non negative and less than the size of collections.

    Please suggest.

    I suggest basic debugging and troubleshooting.

    You provided incomplete code which makes providing an accurate solution next to impossible.  Maybe this is what you're trying to do?

    public CostAnalysisSummary GetCostAnalysisSummary(CostAnalysisParam rp)
    {
    	List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
       
    	CostAnalysisSummary _lstCostAnalysisSummary = new CostAnalysisSummary();
       
    	var dataset = //Have some data from database
    	if (dataset != null && dataset.Tables.Count > 0)
    		if (dataset.Tables[0].Rows.Count > 0)
    		{
    			
    			if (string.IsNullOrEmpty(rp.AppType))
    			{
    				_lstCostAnalysis = (from DataRow row in dataset.Tables[0].Rows
    										select new CostAnalysis
    										{
    										   //list items binded here with business data.
    										}).ToList();			
    				_lstCostAnalysisSummary.Summary = _lstCostAnalysis;
    				}
    			}
    			
    		}
    
    	return _lstCostAnalysisSummary;
    }

    Friday, May 1, 2020 2:15 PM
  • User-1965208702 posted

    Hi Sir,

    I am not doing the one which code you provided above.

    Below is my complete code:

    public List<CostAnalysisSummary> GetCostAnalysisSummary(CostAnalysisParam rp)
            {
                List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
               
                List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>();
               
                var dataset = //Have some data from database
                if (dataset != null && dataset.Tables.Count > 0)
                    if (dataset.Tables[0].Rows.Count > 0)
                    {
                        
                        if (string.IsNullOrEmpty(rp.AppType))
                        {
                            _lstCostAnalysis = (from DataRow row in dataset.Tables[0].Rows
                                                    select new CostAnalysis
                                                    {
                                                       //list items binded here with business data.
                                                    }).ToList();
                            
                            _lstCostAnalysisSummary[0].Summary.AddRange(_lstCostAnalysis);
                            }
                        }
                        
                    }
    
                return _lstCostAnalysisSummary != null ? _lstCostAnalysisSummary : null;
            }

    The code which you have suggested first is already i tried before then found exception index of out of range.

    Please suggest on the above provided code.

    Friday, May 1, 2020 2:51 PM
  • User475983607 posted

    I'm not sure how to help you.  Your code sample is incomplete and the design is questionable at best. 

    From the looks of you code, I think you'll be interested in learning generics and basic class constructions

    public List<CostAnalysisSummary> GetCostAnalysisSummary(CostAnalysisParam rp)
    {
        List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
    
        List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>()
        {
            new CostAnalysisSummary()
            {
                    Summary = new List<CostAnalysis>()
            }
        };
    
        var dataset = //Have some data from database
    
        if (dataset != null && dataset.Tables.Count > 0)
        {
            if (dataset.Tables[0].Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(rp.AppType))
                {
                    _lstCostAnalysis = (from DataRow row in dataset.Tables[0].Rows
                                        select new CostAnalysis
                                        {
                                            //list items binded here with business data.
                                        }).ToList();
    
                    _lstCostAnalysisSummary[0].Summary.AddRange(_lstCostAnalysis);
                }
            }
        }
    
        return _lstCostAnalysisSummary != null ? _lstCostAnalysisSummary : null;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 1, 2020 3:40 PM
  • User-1965208702 posted

    Hi Sir,

    I know how to use the list, But the scenario i represent here is new for me also. That's why i look for the support team help.

    According to the provided code helped me how to initialize the list object inside master model and through that now the specific list item i am able to add. (Did for Summary List)

    Since we have more than one list object defined in one master model.

    I used the suggested code and added the second list "All" value also like below:

    public List<CostAnalysisSummary> GetCostAnalysisSummary(CostAnalysisParam rp)
    {
        List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
    
        List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>()
        {
            new CostAnalysisSummary()
            {
                    Summary = new List<CostAnalysis>()
            },
             new CostAnalysisSummary()
             {
                    All = new List<CostAnalysis>()
             }
        };
    
        var dataset = //Have some data from database
    
        if (dataset != null && dataset.Tables.Count > 0)
        {
            if (dataset.Tables[0].Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(rp.AppType))
                {
                    _lstCostAnalysis = (from DataRow row in dataset.Tables[0].Rows
                                        select new CostAnalysis
                                        {
                                            //list items binded here with business data.
                                        }).ToList();
    
                    _lstCostAnalysisSummary[0].Summary.AddRange(_lstCostAnalysis);
                }
            }
           //Binding data from another table in dataset
            if (dataset.Tables[0].Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(rp.AppType))
                {
                    var All = (from DataRow row in dataset.Tables[0].Rows
                                        select new CostAnalysis
                                        {
                                            //list items binded here with business data.
                                        }).ToList();
    
                    _lstCostAnalysisSummary[0].All.AddRange(_lstCostAnalysis);
                }
            }
        }
    
        return _lstCostAnalysisSummary != null ? _lstCostAnalysisSummary : null;
    }

    In the above code i added another list item called "All", First i initialized like summary also. then when i tried to bind this List All got error like:

    Object reference not set to an object.

    Please suggest.

    Saturday, May 2, 2020 7:29 AM
  • User475983607 posted

    vicky1

    I know how to use the list, But the scenario i represent here is new for me also. That's why i look for the support team help.

    No.  I provided clear recommendations and code which you refused.  Next, I provided code to your unusual specifications.  It seems you do not understand the C# syntax as you added another CostAnalysisSummary. 

    vicky1

    In the above code i added another list item called "All", First i initialized like summary also. then when i tried to bind this List All got error like:

    Object reference not set to an object.

    Please suggest.

    Set the "All" property to the _lstCostAnalysisSummary[0] not _lstCostAnalysisSummary[1].  Please set aside time to learn the fundamentals before moving forward.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    List<CostAnalysis> _lstCostAnalysis = new List<CostAnalysis>();
    
    List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>()
    {
        new CostAnalysisSummary()
        {
            Summary = new List<CostAnalysis>(),
            All = new List<CostAnalysis>()
        }
    };

    Saturday, May 2, 2020 10:18 AM
  • User-1965208702 posted

    Hi Sir,

    As i understood about below line:

    "No.  I provided clear recommendations and code which you refused.  Next, I provided code to your unusual specifications.  It seems you do not understand the C# syntax as you added another CostAnalysisSummary. "

    You are talking about below code right?

    List<CostAnalysisSummary> _lstCostAnalysisSummary = new List<CostAnalysisSummary>()
    {
        new CostAnalysisSummary()
        {
            Summary = new List<CostAnalysis>()
        },
    new CostAnalysisSummary()
        {
            All = new List<CostAnalysis>()
        }
    };

    If yes, Then i have gone through this code only, My only mistake was the index was not put correctly for each list item.

    e.g: _lstCostAnalysisSummary[0].Summary[AddRange();

    _lstCostAnalysisSummary[1].All[AddRange();

    I agree with you, i have done some mistakes like after your suggested code did not do proper debugging about the wrong index put for second list item. Instead asked again about this error.

    As i mentioned before, This is the new case of me and now i get it.

    Thank you for your valuable support.

    Saturday, May 2, 2020 2:29 PM