How do a linq join of an object to itself?
In the code below I'm trying to join an object to itself. The join kinda works, but it's giving me 4 rows instead of the expected 1 row with both values in the same new object and not 4 copies of the same object. Thanks!!
var values = rptDataPkg.Datasets.Item(0).Result.AsEnumerable()
.Where(f => f.Field<int>("RowType") == 3 &&
f.Field<int>("Category") == 1 &&
((f.Field<int>("ItemID") == -1000) || (f.Field<int>("ItemID") == -1001)))
.Select(f => new
{
joinOn = 1,
Proc = f.Field<string>("Item"),
fieldVal = Convert.ToDecimal(f.Field<decimal?>(field))
}).ToList();
var join = values.Join(values, b1 => b1.joinOn, b2 => b2.joinOn,
(b1, b2) => new
{
inHVal = b1.fieldVal,
offSVal = b2.fieldVal
})开发者_开发知识库;
I suspect your values
list has two entries. Both will have a joinOn
value of 1, so you'll end up with both rows matching themselves and each other. So if the rows are A and B, you'll have
(A, A)
(A, B)
(B, A)
(B, B)
精彩评论