简体   繁体   中英

How to select two column values in my linq to sql query?

I'm beginner in linq to sql this:

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();


but in in this line select g.Select(x=>x.natije) select just x.natije ,i want select another filed x.qaza ,how can i write that?

Simply create an anonymous type:-

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

Since this returns IEnumerable<IGrouping<T,T>> you will have to use two foreach loops to get the data:-

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

Also, you can find the first natije from first group but it may result in error:-

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

You can creat a dynamic object on the select:

       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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM