开发者

How can I further simplify this piece of LINQ code

开发者 https://www.devze.com 2023-01-14 13:28 出处:网络
I have this code that I use to bind to a repeater: Repeater 开发者_高级运维rpt; var q = from t in new[] { 10 }

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

精彩评论

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