MoonKnight
MoonKnight

Reputation: 23831

Selecting Items from Class Hierarchy Using LINQ

All, I have a SQL Database class structure like

public class Database : ISqlObject, IComparable<Database>, IEquatable<Database>
{
    public string Name { get; set; }
    public ISqlObject Parent { get; set; }
    public string Filename { get; set; }
    internal List<Schema> Schemas { get; set; }
    internal List<Table> Tables { get; set; }
    internal List<Constraint> Constraints { get; set; }

    public Database(string name, string filename)
    {
        this.Name = name;
        this.Parent = (ISqlObject)this;
        this.Filename = filename;
    }
    ...
}

protected internal class Table : ISqlObject, IComparable<Table>
{
    public string Name { get; set; }
    public ISqlObject Parent { get; set; }
    public List<Column> Columns { get; set; }
    ...
}

protected internal class Column : ISqlObject, IComparable<Column>
{
    public string Name { get; set; }
    public ISqlObject Parent { get; set; }
    public string Type { get; set; }
    public int MaxLength { get; set; }
    public bool IsNullable { get; set; }
    public bool HasConstraints { get; set; }
    public List<Constraint> Constraints { get; set; }
    ...
}

protected internal class Constraint : IComparable<Constraint>
{
    public string Name { get; set; }
    public ISqlObject Parent { get; set; }
    public bool IsPk { get; set; }
    public string LinkedConstraintName { get; set; }
    public string LinkedColumnName { get; set; }
    public string LinkedTableName { get; set; }
    ...
}

Where the elipsis represent the IComparable implementation and constructors etc.

I have the following code to return the corresponding constraint for current column, using LINQ, but I am consistantly getting linkedConstraints = null

foreach (SqlServer.Column column in tableIndexDict[i].Columns)
{
    if (column.HasConstraints)
    {
        var linkedConstraints = 
            from table in database.Tables 
            from c in table.Columns 
            from con in c.Constraints 
                where con.LinkedTableName.Equals(tableIndexDict[i].Name) && 
                        con.LinkedColumnName.Equals(column.Name) 
            select con;
        foreach (SqlServer.Constraint c in linkedConstraints) <= HERE NullReference Exception.
        {

What am I doing wrong here?

Note: Stack trace:

at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at UserCost.Export.QlikViewExporter.BuildExclusionTable(Database database) in 
f:\UserCost\UserCost\UserCost\Export\SqlDataExportFactory.cs:line 302

Upvotes: 0

Views: 180

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

linkedConstraints never can be null in this scenario, because it is lambda.

You can examine is there linked constraints using this:

linkedConstraints.Any();

EDIT

You must examine data you are retrieved. I Assume the con.LinkedTableName or con.LinkedColumnName are null.

Upvotes: 2

Related Questions