karra
karra

Reputation: 378

C# - Linq join returns unexpected number of elements

I have data from two data structures that I would like to join with respect to date. Each data structure contains 88 values and every date in one structure has a corresponding date in the other structure. However, when I try to join them, the list with the joined result contains 90 values. In other words the result contains two extra values. When I inspect the values in the joined list it seems that it contains two extra values at the start of the list that are identical to the first "expected" joined value. Any ideas what might be wrong? Here is the join expression that I am using:

//Joins vib and RPM with respect to date
var joinedVibRPM = serie.Value.Values.Join(
                    RPMSeriesOne.Values,
                    _vib => _vib.DateTime,
                    _rpm => _rpm.DateTime,
                    (_vib, _rpm) => new { vib = _vib.Value, rpm = _rpm.Value }).ToList();

Upvotes: 0

Views: 110

Answers (2)

Sefe
Sefe

Reputation: 14007

You can explain the result when your input sets have duplicate entries. Consider for example this join of two list with four items each (using strings, not DateTime for better readability):

var items1 = new { "A", "B", "C", "C" };
var items2 = new { "A", "B", "B", "C" };

If you perform this join:

var joinedItems =
    from item1 in items1
    join item2 in items2 on item1 equals item2
    select item1 + item2;

Your result will be:

{ "AA", "BB", "BB", "CC", "CC" }

You will find "BB" twice because it is repeated in the secod list and "CC" twice because it is repeated in the first list. In total you will get 5 items.

Upvotes: 2

connectedsoftware
connectedsoftware

Reputation: 7087

If you have any duplicate dates in either structure then one element in one structure will match 2 (or more) elements in the other structure. This will give you more than 88 results.

Check your structures for distinct values:

serie.Value.Values.Distinct().Count();

and

RPMSeriesOne.Values.Distinct().Count();

One of these results will likely be less than 88 indicating the presence of duplicates.

Upvotes: 2

Related Questions