C# 使用LINQ填充列表
我有一个类型为C# 使用LINQ填充列表,c#,linq,arraylist,C#,Linq,Arraylist,我有一个类型为RawResults的数组列表,其中RawResults是一个位置和一个日期 public class RawResult { public string location { get; set; } public DateTime createDate {get; set; } public RawResults(string l, DateTime d) { this.location = l; this.cre
RawResults
的数组列表,其中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填充一个列表,其中包含每个不同的位置以及它在我的arraylist中出现的次数。如果我能在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
中返回的数据,以获得不同的结果集。我有一个列表可供查询。您可以执行以下操作:
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();
尽管列表
不是必需的,除非您绝对需要它成为列表
。要按时间过滤,您可以执行以下操作:
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();
var results = bandwithArrayList
.Cast<RawResults>()
.GroupBy(bw => bw.location, (k, g) => new { location = k, Count = g.Count() })
.ToList();
或使用流利的语法:
var results = data.bandwith
.GroupBy(bw => bw.location, (k, g) => new { location = k, Count = g.Count() })
.OrderBy(r => r.location);
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);
要按日期筛选,只需使用以下选项:
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()
};
List results=MethodToGetResults();
var locationCount=结果
.GroupBy(r=>r.location)
.Select(lc=>new{Location=lc.Location,Count=lc.Count()});
在任何事情之前:使用列表
。谢谢,我将类型改回列表
谢谢你的回答,但是我只有数组列表rawData
来运行LINQ。@Mike你的问题引用了媒体日志
,所以我认为这很重要。如果它根本不相关,您可以将其从查询中删除。请参阅我的更新答案。@newStackExchangeInstance很好,我的答案中包含了一个关于如何使用ArrayList
的注释。很抱歉,我弄糊涂了。我将更新我的问题以包含更多信息。@Mike OK。我的第二个解决方案,或者说,应该对你有用。
var results = bandwithArrayList
.Cast<RawResults>()
.GroupBy(bw => bw.location, (k, g) => new { location = k, Count = g.Count() })
.ToList();
List<RawResult> results = MethodToGetResults();
var locationCount = results
.GroupBy(r => r.location)
.Select(lc => new {Location = lc.location, Count = lc.Count()});