如何在我的linq to sql查询中选择两个列值?

时间:2015-11-20 10:36:15

标签: c# linq linq-to-sql

我是linq to sql的初学者:

var query = (from p in behzad.STATUS
                         select p);

            var taskCounts = (from worker in query.ToList()
                              group worker by worker.date into g
                              select g.Select(x=>x.natije) 
                                ).ToArray();


但是在这一行中select g.Select(x=>x.natije)只选择x.natije,我想选择另一个提交的x.qaza,我该怎么写呢?

2 个答案:

答案 0 :(得分:1)

只需创建一个匿名类型: -

 var taskCounts = (from worker in query
                   group worker by worker.date into g
                   select g.Select(x=> new { natije = x.natije, qaza = x.qaza } );

由于这会返回IEnumerable<IGrouping<T,T>>,您将不得不使用两个foreach循环来获取数据: -

foreach (var item in taskCounts)
{
    foreach (var x in item)
    {
         tempo = x.natije  //here
    }
}

此外,您可以从第一组找到第一个natije,但可能会导致错误: -

 string tempo = res.First().First().natije;

答案 1 :(得分:0)

您可以在选择中创建动态对象:

       var query = (from p in behzad.STATUS
                     select p);

        var taskCounts = (from worker in query.ToList()
                          group worker by worker.date into g
                          select g.Select(x=> new  {  Natije = x.natije,  Qaza = x.qaza }) 
                            ).ToArray();