日期:2014-05-20  浏览次数:20914 次

求教关于linq IQueryable类型取值和类型转换的问题
数据访问这里的代码:
public IQueryable LoadConfig(int groupId)
{
    DataAccessDataContext myDc = new DataAccessDataContext();
    var config = from configs in myDc.MyConfig
                        where configs.groupid == groupId
                        select new
                        {
                            configs.id,
                            configs.varname,
                            configs.type,
                            configs.description,
                            configs.value
                        };
    return config;
}

这样在读取数据的时候得到的结果 IQueryable myConfig = LoadSysConfig(3); 怎么取到myConfig里面的每个值呢?这样读出的myConfig是一个匿名类型的,又该怎么可以将其转换成其他类型,比如我Model层里面的Congfig模型?

通过foreach读取里面的值是这样的:
{ id = 32, varname = isvalidatecode, type = bool, description = 是否开启验证码, value = true }{ id = 33, varname = codetype, type = int, description = 验证码类型, value = 3 }{ id = 34, varname = imgtype, type = string, description = 验证码图片文件类型, value = gif }{ id = 35, varname = fgnoise, type = int, description = 前景噪音干扰点数量, value = 100 }{ id = 36, varname = bgnoise, type = int, description = 背景噪音干扰线数量, value = 30 }{ id = 37, varname = codewidth, type = int, description = 验证码图片宽度, value = 100 }{ id = 38, varname = codeheight, type = int, description = 验证码图片高度, value = 30 }{ id = 40, varname = bgcolor, type = string, description = 验证码背景颜色(输入0则表示背景颜色随机), value = 0 }{ id = 41, varname = bgnoisecolor, type = string, description = 背景干扰线颜色, value = #FF8080 }{ id = 42, varname = bgnoisewidth, type = float, description = 背景干扰线宽度, value = 1.2f }{ id = 43, varname = codefontsize, type = int, description = 验证码字体大小, value = 14 }{ id = 44, varname = codefontstyle, type = string, description = 验证码字体样式, value = Bold, Italic, Strikeout }{ id = 45, varname = codecolor1, type = string, description = 验证码颜色渐变起点色, value = #0000FF }{ id = 46, varname = codecolor2, type = string, description = 验证码颜色渐变终点色, value = #FF0000 }{ id = 47, varname = changeangle, type = float, description = 验证码颜色渐变改变角度, value = 1.2f }{ id = 48, varname = isdistortion, type = bool, description = 文字是否变形, value = false }{ id = 49, varname = codelength, type = int, description = 验证码字符个数, value = 4 } 

也就是相当于每行就是一个Config的对象,但我究竟要怎么得到它这个具体的值呢?

------解决方案--------------------

//自定义类来实现
public List<Config> LoadConfig(int groupId)
{
  DataAccessDataContext myDc = new DataAccessDataContext();
  var config = from configs in myDc.MyConfig
  where configs.groupid == groupId
  select new Config
  {
  id=configs.id,
  varname=configs.varname,
  type=nfigs.type,