Solved

LINQ, C# -- easy "Generic.List" fix ?

Posted on 2013-12-30
1
360 Views
Last Modified: 2016-02-10
Below "var genres" WORKS, displaying the attached.
How can I get the below "var genres2" to display a LIST ?

When I run, a
Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<char>>' to 'System.Collections.Generic.List<string>'. An explicit conversion exists (are you missing a cast?) error displays.

The "PartNumber" database column is varchar(50)
------------------------------------------------------------------------------------
   public ActionResult Index()
        {
            var genres = new List<string> { "Rock", "Jazz", "Country", "Pop", "Hip", "Disco" };

            var genres2 = from o in db.Lookup_PartNumbers
                          select o.PartNumber.ToList();

            // Create our view model
            var viewModel2 = new StoreIndexViewModel
            {
                NumberOfGenres = genres.Count(),
                Genres = genres,
                Test = genres.Count()
            };

            return this.View(viewModel2);          
        }
0
Comment
Question by:finance_teacher
1 Comment
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Try moving the ToList call outside of the LINQ query:

var genres2 = (from o in db.Lookup_PartNumbers
               select o.PartNumber).ToList();

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
grab and show facebook pages event calendars 6 40
c# code 19 56
Code enhancement 5 12
Hide Tab Page 3 18
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

11 Experts available now in Live!

Get 1:1 Help Now