1

I'm new to linq. I'm using linq to fill some classes from the database.

All my objects are being filled correctly using my linq except for one small thing. Below you can see that for "SAR" "Flight Direction" (rows 15,16) I should have a value for Ascending and a value for Descending. But what I'm getting are two value objects for "Ascending".

Why isn't it looping through my values? What is the LINQ missing?

Here are my DB results:

enter image description here

Here is my LINQ:

using (NpgsqlDataReader reader = command.ExecuteReader())
{

    if (reader.HasRows)
    {

        var groups = reader.Cast<System.Data.Common.DbDataRecord>()
            .GroupBy(dr => new { ID = (int)dr["id"], AttID = (int)dr["AttId"] })
            .GroupBy(g => g.Key.ID);

        typeDataList = (
            from typeGroup in groups
            let typeRow = typeGroup.First().First()
            select new TypeData()
            {
                ID = (int) typeRow["id"],
                Type = (string) typeRow["type"],
                Attributes = 
                (
                    from attGroup in typeGroup
                    let attRow = attGroup.First()
                    select new TypeDataAttribute()
                    {
                        ID = (int)attRow["AttId"],
                        Label = (string)attRow["label"],
                        PossibleValues =
                        (
                            from row in attGroup
                            where !DBNull.Value.Equals(attRow["AttValueId"])
                            select new TypeDataAttributeValue() { ID = (int)attRow["AttValueId"], Value = (string)attRow["value"] }
                        ).ToArray()
                    }
                ).ToArray()
            }
        );
    }
}
Community
  • 1
  • 1
capdragon
  • 14,565
  • 24
  • 107
  • 153

1 Answers1

2

You are taking Value from attRow, you should take it from row.

Wasp
  • 3,395
  • 19
  • 37