ASP.net C# LINQ for 循环

发布于 2024-10-24 09:50:35 字数 449 浏览 1 评论 0原文

// Load all the links
ArtworkingDataContext dc = new ArtworkingDataContext();
var q = (from Labels in dc.tblArtworkLabels where Labels.templateID == this.ID select new { LabelID = Labels.ID });

// Create labels array
this.Labels = new ArtworkLabel[q.Count()];
for (int i = 0; i < q.Count(); i++)
{
    this.Labels[i] = new ArtworkLabel(q.LabelID);
}

q.LabelID 不起作用,我不能真正使用 foreach,因为我必须在每次迭代时调用一个新的 ArtworkLabel。

// Load all the links
ArtworkingDataContext dc = new ArtworkingDataContext();
var q = (from Labels in dc.tblArtworkLabels where Labels.templateID == this.ID select new { LabelID = Labels.ID });

// Create labels array
this.Labels = new ArtworkLabel[q.Count()];
for (int i = 0; i < q.Count(); i++)
{
    this.Labels[i] = new ArtworkLabel(q.LabelID);
}

The q.LabelID isn't working, I can't really use a foreach because I have to invoke a new ArtworkLabel on each iteration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

囍笑 2024-10-31 09:50:35

这可行:

var queryList = q.ToList();
for (int i = 0; i < queryList.Count; i++)
{
    this.Labels[i] = new ArtworkLabel(queryList[i].LabelID);
}

您也可以直接从查询中进行投影:

this.Labels = dc.tblArtworkLabels
                .Where( x=> x.templateId == this.Id)
                .Select( x=> new ArtworkLabel(x.ID))
                .ToArray();

This would work:

var queryList = q.ToList();
for (int i = 0; i < queryList.Count; i++)
{
    this.Labels[i] = new ArtworkLabel(queryList[i].LabelID);
}

Also you can directly project from your query:

this.Labels = dc.tblArtworkLabels
                .Where( x=> x.templateId == this.Id)
                .Select( x=> new ArtworkLabel(x.ID))
                .ToArray();
韶华倾负 2024-10-31 09:50:35

尝试这样:

this.Labels = q.Select(x => new ArtworkLabel(x.LabelID)).ToArray();

Try like this:

this.Labels = q.Select(x => new ArtworkLabel(x.LabelID)).ToArray();
情归归情 2024-10-31 09:50:35

你可能会这样做,对吧?

ArtworkingDataContext dc = new ArtworkingDataContext();

this.Labels = 
    from label in dc.tblArtworkLabels 
    where label.templateID == this.ID
    select new ArtworkLabel(label.ID).ToArray();

You could potentially just do this, right?

ArtworkingDataContext dc = new ArtworkingDataContext();

this.Labels = 
    from label in dc.tblArtworkLabels 
    where label.templateID == this.ID
    select new ArtworkLabel(label.ID).ToArray();
ぺ禁宫浮华殁 2024-10-31 09:50:35

您应该能够在一个 linq 语句中完成这一切。

ArtworkingDataContext dc = new ArtworkingDataContext();
this.Labels = (from Labels in dc.tblArtworkLabels where Labels.templateID == this.ID select new ArtworkLabel(Labels.ID)).ToArray();

You should be able to do it all in one linq statement.

ArtworkingDataContext dc = new ArtworkingDataContext();
this.Labels = (from Labels in dc.tblArtworkLabels where Labels.templateID == this.ID select new ArtworkLabel(Labels.ID)).ToArray();
年少掌心 2024-10-31 09:50:35

我最近尝试了这个,一个 linq for 循环..这就是我想出的..

        var query = from ii in Enumerable.Range(0, xml.AllIndexesOf("<item").Count())
                    where ii < xml.AllIndexesOf("<item").Count()
                    select new { Start = xml.AllIndexesOf("<item").ToList()[sii], Count = xml.AllIndexesOf("</item").ToList()[sii] - xml.AllIndexesOf("<item").ToList()[sii] };

I tried this recently, a linq for loop.. this is what I came up with..

        var query = from ii in Enumerable.Range(0, xml.AllIndexesOf("<item").Count())
                    where ii < xml.AllIndexesOf("<item").Count()
                    select new { Start = xml.AllIndexesOf("<item").ToList()[sii], Count = xml.AllIndexesOf("</item").ToList()[sii] - xml.AllIndexesOf("<item").ToList()[sii] };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文