【问题标题】:Populate list using LINQ使用 LINQ 填充列表
【发布时间】:2013-06-15 02:52:06
【问题描述】:

我有一个 RawResults 类型的 ArrayList,其中 RawResults 是位置和日期

public class RawResult
{
    public string location { get; set; }
    public DateTime createDate {get; set; }

    public RawResults(string l, DateTime d)
    {
        this.location = l;
        this.createDate = d;
    }
}

我想使用 LINQ 填充一个列表,其中包含每个不同的位置以及它在我的数组列表中出现的次数。如果我能够在 SQL 中做到这一点,它会是这样的

select 
   bw.location, 
   count(*) as Count
from 
   bandwidth bw, 
   media_log ml
where
   bw.IP_SUBNET = ml.SUBNET
   group by bw.location
   order by location asc

稍后我也必须在给定的日期范围内做同样的事情。

更新 这是为获取 rawData 中的所有数据而运行的查询

SELECT        
    MEDIASTREAM.BANDWIDTH.LOCATION, MEDIASTREAM.MEDIA_LOG.CREATE_DATE
FROM            
    MEDIASTREAM.BANDWIDTH INNER JOIN
      MEDIASTREAM.MEDIA_LOG ON MEDIASTREAM.BANDWIDTH.IP_SUBNET =     
      MEDIASTREAM.MEDIA_LOG.SUBNET

现在我需要查询rawData 中返回的数据以获取不同的结果集。我有一个可供查询的列表。

【问题讨论】:

  • 在任何事情之前:使用List<RawResult>
  • 谢谢,我把类型改回List<RawResult>

标签: c# linq arraylist


【解决方案1】:

你可以这样做:

var results = 
    (from bw in data.bandwith
     join ml in data.media_log on bw.IP_SUBNET equals ml.SUBNET
     group bw by bw.location into g
     orderby g.Key
     select new 
     { 
         location = g.Key, 
         Count = g.Count() 
     })
    .ToList();

尽管ToList 不是必需的,除非您绝对需要它是List<T>。要按时间过滤,您可以这样做:

var results = 
    (from bw in data.bandwith
     join ml in data.media_log on bw.IP_SUBNET equals ml.SUBNET
     where bw.createDate >= minDate && bw.createDate <= maxDate
     group bw by bw.location into g
     orderby g.Key
     select new 
     { 
         location = g.Key, 
         Count = g.Count() 
     })
    .ToList();

如果media_log 不相关,您可以省略join

var results = 
    from bw in data.bandwith
    group bw by bw.location into g
    orderby g.Key
    select new 
    { 
        location = g.Key, 
        Count = g.Count() 
    }

或者用流利的语法:

var results = data.bandwith
    .GroupBy(bw => bw.location, (k, g) => new { location = k, Count = g.Count() })
    .OrderBy(r => r.location);

要按日期过滤,只需使用:

var results = 
    from bw in data.bandwith
    where bw.createDate >= minDate && bw.createDate <= maxDate
    group bw by bw.location into g
    orderby g.Key
    select new 
    { 
        location = g.Key, 
        Count = g.Count() 
    };

或者用流利的语法:

var results = data.bandwith
    .Where(bw => bw.createDate >= minDate && bw.createDate <= maxDate)
    .GroupBy(bw => bw.location, (k, g) => new { location = k, Count = g.Count() })
    .OrderBy(r => r.location);

注意,要在 Linq 查询中使用 ArrayList 或任何其他非泛型集合类型,请使用 Cast&lt;T&gt;OfType&lt;T&gt; 方法,如下所示:

var results = bandwithArrayList
    .Cast<RawResults>()
    .GroupBy(bw => bw.location, (k, g) => new { location = k, Count = g.Count() })
    .ToList();

【讨论】:

  • 感谢您的回复,但我只有 ArrayList&lt;RawResults&gt; rawData 可以运行 LINQ。
  • @Mike 你的问题引用了media_log,所以我认为这很重要。如果它根本不相关,您可以将其排除在查询之外。请参阅我的更新答案。
  • @newStackExchangeInstance 好点,我在回答中包含了有关如何使用ArrayList 的说明。
  • 很抱歉让您感到困惑。我将更新我的问题以包含更多信息。
  • @Mike 好的。我的第二个解决方案,或者EkoostikMartin 应该适合你。
【解决方案2】:
List<RawResult> results = MethodToGetResults();

var locationCount = results
     .GroupBy(r => r.location)
     .Select(lc => new {Location = lc.location, Count = lc.Count()});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多