Solved

How To Return a Model From a ToList();

Posted on 2013-05-22
3
364 Views
Last Modified: 2013-05-22
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!!
0
Comment
Question by:allanau20
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 39189261
private static List<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.........
}


You're returning a list of MdlProd.

If you wanted a single instance you would use .FirstOrDefault instead of ToList
0
 
LVL 5

Author Comment

by:allanau20
ID: 39189268
gotcha ged325; I'm learning and thanks!
0
 
LVL 5

Author Closing Comment

by:allanau20
ID: 39189269
thanks for the quick answer!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now