Suppose I have an output table like this:
Names | Values
_______________
A | value1
A | value2
B | value3
B | value4
C | value5
I use dapper to connect to the database. Instead of direct sql commands, I call a stored procedure that has multiple queries so I use QueryMultipleAsync
to get the results but I got stuck on this one. For the sake of this example, the task returns only this one.
public async Task<Something> GetData()
{
using (IDbConnection database = _mssqlDbConnectionFactory.Connect())
{
var results = await database.QueryMultipleAsync("spGetSomething");
...
IEnumerable<Something> something = results.Read< > ...
return something
}
}
public class Something
{
public string? Name { get; set; }
public List<string>? Values { get; set; }
}
Besides, I use sql server and the query for this output is kinda long, so I didn't include it here, and if I'm correct, there is no way of grouping them together, since I need every value. What are my options here? Can I use some kind of linq to group them when I read the result?
CodePudding user response:
You can try to use a class as DataModel
to relay your data from DB.
public class DataModel
{
public string? Name { get; set; }
public string Values { get; set; }
}
Then use lambda Groupby
IEnumerable<Something> something = results.Read<DataModel>()
.GroupBy(x=> x.Name)
.Select(x=> new Something(){
Name = x.Key,
Values = x.Select(y=>y.Values)
});