Avatar of Allan
Allan
Flag for United States of America

asked on 

How To Return a Model From a ToList();

Hi Experts!

Thanks for reading this.

How do you return Model1(MdlProd) from the function GetProduct?


The error msg is:

(local variable)List<Model1>MdlProd

Error: Cannot implicitly convert type 'System.Collections.Generic.List<SomeNameSpace.DomainClasses.Model1>' to 'SomeNameSpace.DomainClasses.Model1'


Here are the two methods:
public static int Process()
{
    var MdlProduct = GetProduct();
}

private static Model1 GetProduct()
{           
    try
    {
        var MdlProds = (from x in Something
                   select x).ToList();

        var MdlProd = (from p in MdlProds
                     group p by p.PRODUCT into pGroup
                     select new Model1()
                     {blah, blah;
                     }).ToList();

        return (MdlProd);        <-- ERROR
    }
        catch.........
}

Open in new window

Here is the Model1:
public class Model1
{
    public string PRODUCT { get; set; }
    public string STARTDATE { get; set; }
    public string ENDDATE { get; set; }
}

Open in new window


TIA!!
.NET ProgrammingC#SQL

Avatar of undefined
Last Comment
Allan

8/22/2022 - Mon