I am trying to join 2 arrays but getting exception with linq inner join:
Object reference not set to an instance of an object.
Code :
public class model
{
public string Id { get; set; }
public string Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
var sourceData = GetData(SourceConnectionstring,
"select Id,Value from source");
var targetData = GetData(TargetConnectionstring,
"select Id,Value from target");
var data = (from s in sourceData
join t in targetData on s.Id equals t.Id
where s.Value != t.Value
select new
{
srcKey = s.Id,
srcSnapshot = s.Value,
tgtKey = t.Id,
tgtSnapshot = t.Value
}).ToArray();
}
public static model[] GetData(string connectionString, string sqlQuery)
{
model[] m = new model[1000];
using (var sqlConnection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(sqlQuery, sqlConnection))
{
sqlConnection.Open();
using (var reader = command.ExecuteReader())
{
var id = reader.GetOrdinal("Id");
var value = reader.GetOrdinal("Value");
int c = 0;
while (reader.Read())
{
model n = new model
{
Id = reader.IsDBNull(id) ? null : reader.GetString(id),
Value = reader.IsDBNull(value) ? null : reader.GetString(value)
};
m[c] = n;
c = c + 1;
}
reader.Close();
}
}
sqlConnection.Close();
}
return m;
}
}
Getting Error on t.Id:
equals t.Id
I want to join 2 arrays based on Id and get records whose Value is not matching.
I am not getting whats the problem.
Can anybody please help me with this?