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
Solved

How To Return a Model From a ToList();

Posted on 2013-05-22
3
370 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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
Merge two rows in SQL 4 25
TSQL Query Into Specific XML Format w/ Multiple Groupings 6 33
Need a simple RegEx to search for two words 27 41
TSQL Challenge... 7 35
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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