LINQ到SQL选择多列 [英] Linq to SQL select multiple columns

查看:113
本文介绍了LINQ到SQL选择多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想使用LINQ从MSSQL数据库中选择2列。



SQL应

 选择table.col1,从表
table.col2

我试过

 的IList<串GT; myResults = 
(从dbconn.table
数据,其中table.col5 == NULL
选择新{
COL1 = data.Id.ToString(),$
b $ b COL2 = data.col2
}
)。取(20).ToList();



但这并没有工作。



它说

 不能转换类型列表< AnonymousType#1>到IList<串GT; 


解决方案

您基本上是试图填补的字符串与列表匿名类型列表中的条目,这是行不通的。



你有没有尝试过这样的事情:

 无功名单=从dbconn.table 
数据,其中table.col5 == NULL
选择新{
COL1 = data.Id.ToString (),
COL2 = data.col2
}

然后你可以很容易地使用中的条目例如环

 的foreach(在列表VAR元素){
// ...
}

或者像一个列表

  list.Take(20).ToList(); 


I just want to select 2 columns from a MSSQL DB using LINQ.

The SQL should be

select table.col1,table.col2 from table

I tried

IList<string> myResults =
(
    from data in dbconn.table
    where table.col5 == null
    select new { 
        col1=data.Id.ToString(),
        col2=data.col2
    }
).Take(20).ToList();

but this didn't work.

It says

cannot convert type  list <AnonymousType#1> to Ilist<string>

解决方案

You are basically trying to fill a list of strings with the entries of a list of anonymous types, that won't work.

Have you tried something like this?:

var list = from data in dbconn.table
           where table.col5 == null
           select new { 
            col1=data.Id.ToString(),
            col2=data.col2
           }

Then you can easily use the entries in a loop for example

foreach(var element in list) {
//...
}

Or like a list

list.Take(20).ToList();

这篇关于LINQ到SQL选择多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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