将Linq转换为SQL列表以列出 [英] Cast Linq to SQL List to List

查看:106
本文介绍了将Linq转换为SQL列表以列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这应该很简单,但是..

我对SQL查询有一个要求:

I know this should be straight forward but..

I have a Ling to SQL query:

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:

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
};



当我用catModel的Linq结果替换viewModel中的"items"时,我会遇到各种类型的转换错误.无论我做什么尝试,都无法将catQuery类型转换为List< item>的IQueryable类型.

有人对我想念的东西有什么想法吗?



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?

推荐答案

您是否尝试过以下方法:

have you tried this:

List<Item> catModel = new List<Item>();
catModel.AddRange((from BU in CTMapDB.TBL_DIM_BU
                   where BU.si_BUId > 0
                   select BU).ToArray());


这终于奏效了:

This is what finally worked:

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


这篇关于将Linq转换为SQL列表以列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆