LINQ能否直接类型转换
以下问题,下述写法需要建立一个lll序列逐步便利,能否让
var NewList = from p in BigList select new Small { id = p.id };直接转换为List<int>
即:List<int> NewList = from p in BigList select new { id = p.id };,但是这么写不好用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web
{
class Article
{
public int? id;
public string nr;
}
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Article> ArticleList = new List<Article>();
ArticleList.Add(new Article { id = 1, nr = "11" });
ArticleList.Add(new Article { id = 2, nr = "22" });
ArticleList.Add(new Article { id = 3, nr = "33" });
ArticleList.Add(new Article { id = 4, nr = "44" });
ArticleList.Add(new Article { id = 5, nr = "55" });
//NewList是对ArticleList的加工
var NewList = from p in ArticleList where p.id<=3 select new { id = p.id };
List<int> lll = new List<int>();
foreach (var p in NewList)
{
lll.Add(Convert.ToInt32(p.id));
}
//问题:上述写法需要建立一个lll序列逐步便利,能否让
//var NewList = from p in BigList select new Small { id = p.id };直接转换为List<int>
//即:List<int> NewList = from p in BigList select new { id = p.id };,但是这么写不好用