Solved

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

Posted on 2013-12-30
1
366 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39746331
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

820 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