3

I'm using dapper Query method to fetch a set of data from sqlite with left join, it does return the column i need but multiple times.

i tried these query on DB Browser it works fine,

 string sql =
         @"select a.id, a.alert_tag, lyr.layer_name, ln.line_name, t.task_name from alert_tag a
              LEFT JOIN layer_group lyr on lyr.layerID = a.layer_group
              LEFT JOIN line ln on ln.lineID = a.line
              LEFT JOIN task t on t.taskID = a.task";

then i call it,

using (IDbConnection cnn = new SQLiteConnection(Tools.LoadConnectionString()))
{
    var output = cnn.Query<dynamic>(sql);
    return output.ToList();
}
List<dynamic> Alerttag_List = new List<dynamic>();

private void LoadDGVdata()
{
     Alerttag_List = SqliteQuery_AlertTagModel.Load();
     dgv_AlertTag.DataSource = Alerttag_List;
}

I expected to get result like these

| id | alert_tag | layer_name | line_name | task_name |

but i got these

| id | alert_tag | layer_name | line_name | task_name | id | alert_tag | layer_name | line_name | task_name |
10
  • What happens when you run the query directly against the database Commented May 15, 2019 at 10:37
  • i got the 5 columns that i wanted, without duplicates. Commented May 15, 2019 at 10:46
  • This sounds like a problem with the grid binding, to be honest. Can you do: var propCount = TypeDescriptor.GetProperties(Alerttag_List[0]).Count and tell me what it says? is it 5, or is it 10? Commented May 15, 2019 at 10:47
  • also: how many rows are there in this example? is it 2 perhaps? I wonder whether the grid is generating a column per unique PropertyDescriptor reference, but the RowBoundPropertyDescriptor that dapper is exposing here is transient (generated on the fly per row). Also: what kind of grid is that? i.e. is this DataGridView ? or...? And: does this perhaps gain columns, i.e. you have AutoGenerateColumns (or whatever) enabled, and it is adding them whenever you change the source? Commented May 15, 2019 at 10:50
  • Side note: generating a POCO class and mapping to that (i.e. Query<YourType>) should be very reliable - the dynamic property API via PropertyDescriptor however, is... twitchy Commented May 15, 2019 at 10:52

1 Answer 1

1

You can use: ExecuteReader. it will return single data. Example:

public dynamic GetTableData(string schemaName, string tableName)
        {
            string sql = $@"select * from {schemaName}.{tableName}";
            var tableData = _sqlConnection.ExecuteReader(sql);
            return tableData;
        }

Debug Pic

Sign up to request clarification or add additional context in comments.

3 Comments

ExecuteReader is not compatible with dynamic AFAIK (context: I wrote it). And even if it was, I don't think that is the problem here.
ExecuteReader from dapper. I have already tested. and edited the answer with a picture.
Hello Marc, Maybe same thing but for bindable grid/treelist problem happens. stackoverflow.com/questions/66096906/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.