简体   繁体   English

选择Linq中的子查询

[英]Subquery in select Linq

My query is as below 我的查询如下

var result = from cr in cumulativeresult
    orderby cr.Days
    select new
    {
        Days = cr.Days,
        Com = (from cr1 in cumulativeresult
            where cr1.Days < cr.Days
            group cr1 by 1 into cr1grp
            select
            new
            {
                Count = cr1grp.Count() + cr.Com 
            }),
        ComPercent = Math.Round((double.Parse((
            from cr1 in cumulativeresult
            where cr1.Days < cr.Days
            group cr1 by 1 into cr1grp 
            select cr1grp.Count() + cr.Com).ToString()) / ComCount) * 100, 2)
    };

But the Com and ComPercent are not coming as values. 但是Com和ComPercent并不是作为价值来的。 How will I be able to retrieve the value within them? 我如何能够检索其中的值? My loop for retrieving them is as follows 我检索它们的循环如下

Update 更新资料

foreach (var item in result)
{
    dt.Rows.Add(item.Days, item.Com, item.ComPercent);
}

I do not want to do any processing on my foreach loop, I want the value in my linq query itself. 我不想在foreach循环上进行任何处理,我希望linq查询本身中的值。

Compiler doesn't know your subquery will always return one row, since the return type is still IEnumerable (or IQueryable ). 编译器不知道您的子查询将始终返回一行,因为返回类型仍然是IEnumerable (或IQueryable )。 This can be solved by using First() or Single() (or the corresponding ..OrDefault() version), however, it is more common to just use Count() without grouping, for example : 这可以通过使用First()Single() (或相应的..OrDefault()版本)解决,但是,更常见的是仅使用Count()而不进行分组,例如:

Com = (from cr1 in cumulativeresult
       where cr1.Days < cr.Days
       select cr1
      ).Count() + cr.Com,

You might also want to consider moving the subquery into a let clause, so it can be reused to calculate Com and ComPercent : 您可能还需要考虑将子查询移到let子句中,以便可以重新使用它来计算ComComPercent

var result = from cr in cumulativeresult
             let totalCount = 
                      (from cr1 in cumulativeresult
                       where cr1.Days < cr.Days
                       select cr1
                      ).Count() + cr.Com 
             orderby cr.Days
             select new
             {
                Days = cr.Days,
                Com = totalCount,
                ComPercent = Math.Round((double)totalCount / ComCount * 100, 2)
             };

To answer your question, you need to select the .First of what you, but not Linq, knows is a single grouping of 1 . 要回答你的问题,你需要选择.First的是你,而不是LINQ的,知道是一个单一的分组1

I've adjusted your query in a couple of other ways as well: 我还通过其他几种方式调整了您的查询:

  1. To get some output I had to change the < to <= . 为了获得一些输出,我必须将<更改为<=
  2. I used a cast instead of reparsing as double. 我使用了强制转换而不是将其重整为两倍。

This is the whole LinqPad query: 这是整个LinqPad查询:

var cumulativeresult = new[] { new { Days = 0, Com = 0 }, new { Days = 1, Com = 1 } };
var ComCount = cumulativeresult.Count();
var result = from cr in cumulativeresult
             orderby cr.Days
             select new
             {
                 Days = cr.Days,
                 Com = (from cr1 in cumulativeresult
                        where cr1.Days <= cr.Days
                        group cr1 by 1 into cr1grp
                        select
                          new
                          {
                              Count = cr1grp.Count() + cr.Com
                          }
                           ).First().Count,
                 ComPercent = Math.Round((//double.Parse(
                           ((double)(
                           from cr1 in cumulativeresult
                           where cr1.Days <= cr.Days
                           group cr1 by 1 into cr1grp
                           select cr1grp.Count() + cr.Com).First())//.ToString())
                           / ComCount) * 100, 2
                           )
             };

var dt = new DataTable();
dt.Columns.AddRange(new[] {new DataColumn("Days"),new DataColumn("Com"),new DataColumn("ComPercent")  });
foreach (var item in result)
{
    dt.Rows.Add(item.Days, item.Com, item.ComPercent);
}
dt.Dump();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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