Solved

How To Return a Model From a ToList();

Posted on 2013-05-22
3
371 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 46
TFS 2015 Access denied. 1 30
Problem with MySQL query - graph 3 24
SP result not being displayed 5 40
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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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