开发者

ASP.net c# LINQ for loop

// 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 (i开发者_如何学Gont 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.


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();


Try like this:

this.Labels = q.Select(x => new ArtworkLabel(x.LabelID)).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();


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();


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] };
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜