Solved

How To Return a Model From a ToList();

Posted on 2013-05-22
3
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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