c# - 循环 ienumerable 并获取值

标签 c# linq ienumerable linq-to-objects

我需要从这个 linq 中获取值:

var joinPreRes = (from t1 in db.preguntas_respuestas
                  join t2 in db.respuesta1
                  on t1.id_respuesta equals t2.id
                  where t1.id_pregunta == id
                  group new
                  {
                      t1.id,
                      t1.id_respuesta,
                      t2.respuesta_visual,
                      t2.respuesta_valor
                  } by t1.id);


foreach (var a in joinPreRes)
{


}

如何访问 foreach 上的 id 或 id_respuesta_visual???

最佳答案

您正在创建一个组,因此您应该迭代分组的项目。

var joinPreRes = from t1 in db.preguntas_respuestas
                          join t2 in db.respuesta1
                          on t1.id_respuesta equals t2.id
                          where t1.id_pregunta == id
                          group new
                          {
                              t1.id,
                              t1.id_respuesta,
                              t2.respuesta_visual,
                              t2.respuesta_valor
                          } by t1.id into GroupedItems
                          select GroupedItems;


foreach (var a in joinPreRes)
{
    // a.Key   is the t1.id

    foreach (var subItem in a)
    {
        // subItem.id_respuesta   <-   other fields/properties
        // subItem.respuesta_valor   <-   other fields/properties
    }
}

关于c# - 循环 ienumerable 并获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33785476/

相关文章:

c# - .NET 5 中的 Windows API - Microsoft.Windows.SDK.NET.dll 是 25MB?

c# - 为 sql 中的聚合非重复计数编写一个可比较的 LINQ 查询?

c# - Linq 新语句有条件选择

C#接口(interface)问题

c# - 枚举 DataTable,过滤项目,然后还原为 DataTable

c# - 将 System.Collections.Generic.SortedList<string,T> 转换为 IEnumerable<T>

c# - DataGridTextColumn 可见性绑定(bind)

c# - 通过代理连接到 IRC (.NET)

c# - 以编程方式将标签放在 div 或标签中

c# - 使用 Linq 递归查询 XML