开发者

How can I further simplify this piece of LINQ code

I have this code that I use to bind to a repeater:

Repeater 开发者_高级运维rpt;

var q = from t in new[] { 10 }
        select new { ID = t };

rpt.DataSource = q;
rpt.DataBind();

Is there a simpler way to accomplish this code segment; the var q part?


Repeater rpt;

rpt.DataSource = new[] { new { ID = 10 } };
rpt.DataBind();


Not really. You could write it like this if you prefer:

var q = new[] { 10 }.Select(t => new { ID = t });
rpt.DataSource = q;
rpt.DataBind();


It doesn't get much simpler than that.

You could inline the variable, so that it becomes:

Repeater rpt = ...;

rpt.DataSource = from t in new[] { 10 }
                 select new { ID = t };
rpt.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜