LINQ newbie question for 2 dimensional set
I have a LINQ query that pulls ID values with integers:
var data = from s in BaseData
select new { s.Roid, s.Tid};
? data.ToArray()
{<>f__AnonymousType0<int,int>[14]}
[0]: { Roid = 2384, Tid = 1 }
[1]: { Roid = 2384, Tid = 2 }
[2]: { Roid = 2384, Tid = 3 }
[3]: { Roid = 2385, Tid = 4 }
[4]: { Roid = 2385, Tid = 5 }
[5]: { Roid = 2385, Tid = 6 }
[6]: { Roid = 2386, Tid = 1 }
[7]: { Roid = 2386, Tid = 3 }
[8]: { Roid = 2386, Tid = 6 }
[9]开发者_如何学运维: { Roid = 2386, Tid = 7 }
[10]: { Roid = 2387, Tid = 1 }
[11]: { Roid = 2387, Tid = 2 }
[12]: { Roid = 2387, Tid = 4 }
[13]: { Roid = 2387, Tid = 9 }
What I would like to do is to query the result set for an Roid and extract only the Tid values.
So I want to query data for Roid = 2387, which will return 1, 2, 4, 9 which I want to convert to an array.
Is something like this possible?
// ***Edit***
var items = data.ToArray().Where(s => s.RepairOrderId == 2387);
// gets me closer, but I still only want the Tid's
// ***Edit***
var tidsFromRoid = data.Where(x => x.Roid == 2387).Select(x => x.Tid);
This will return a subsequence that contains only pairs whose Roid == 2387
, and then from that subsequence, it projects each element onto just its Tid
.
Note that if you want this as an array, call .ToArray()
after:
var arrayOfTids = tidsFromRoid.ToArray();
If you wanted to do this from the very beginning (since maybe you won't ever care about other Roid
s) then you make your original query:
var arrayOfTids = BaseData.Where(x => x.Roid == 2387).Select(x => x.Tid).ToArray();
Using query expression syntax, this can also be written as follows. This should still produce the same IL:
int desiredRepairOrderId = 2387;
var q = from e in data where e.Roid == desiredRepairOrderId select new {e.Tid};
var arrayOfResults = q.ToArray();
精彩评论