Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this should be straight forward but..

I have a Ling to SQL query:

C#
var catModel = from BU in CTMapDB.TBL_DIM_BU
                           where BU.si_BUId > 0
                           select BU.vc_BUName;


which I know want to stuff into a List like so:

CSS
var items = new List<Item>(){
    new Item { ItemName = categories + " Item 1" },
    new Item { ItemName = categories + " Item 2" },
    new Item { ItemName = categories + " Item 3" },
    new Item { ItemName = categories + " Item 4" },
    new Item { ItemName = categories + " Item 5"}
};

var viewModel = new MapBrowseViewModel
{
    Category = categoryModel,
    Item = items
};


When I replace "items" in the viewModel with my Linq result of catModel I get all sorts of casting errors which I would expect. No matter what I try I can't cast catModel, which is of type IQueryable into List<item>.

Anyone have any ideas what I'm missing?
Posted

have you tried this:

C#
List<Item> catModel = new List<Item>();
catModel.AddRange((from BU in CTMapDB.TBL_DIM_BU
                   where BU.si_BUId > 0
                   select BU).ToArray());
 
Share this answer
 
Comments
Don Burton 19-Oct-10 13:13pm    
I just tried and got some push back from the compiler:

The best overloaded method match for 'System.Collections.Generic.List<ctmapping.models.item>.AddRange(System.Collections.Generic.IEnumerable<ctmapping.models.item>)' has some invalid arguments


Argument '1': cannot convert from 'CTMapping.Models.TBL_DIM_BU[]' to 'System.Collections.Generic.IEnumerable<ctmapping.models.item>'

My frustration is all of this works in .NET 4.0 because of covariance. But not in 3.5 which is where I must remain for this project.

Thanks.
This is what finally worked:

C#
var sqlitems = (from BU in CTMap.GetTable<TBL_DIM_BU>()
                            select new Item { ItemName = BU.vc_BUName }).ToList<Item>();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900