[英]Grouping items in Linq query
I'm trying to group a result set of ektron search results items and output them on screen, I use the following code 我正在尝试将ektron搜索结果项目的结果集分组并在屏幕上输出,我使用以下代码
var groupdResults =
from result in response.Results
orderby result[SearchSmartFormProperty.GetDateProperty("/root/FundClosingDate")]
group result by result[SearchSmartFormProperty.GetStringProperty("/root/DeadlineAltText")]
into statusGroup
select new
{
closingDate =statusGroup.Key,
count= statusGroup.Count
};
I then add these to a listview: uxSearchResultView.DataSource = groupdResults; 然后,将它们添加到列表视图中:uxSearchResultView.DataSource = groupdResults;
The problem I'm having is that i need to output all the data from the resultset eg title,closingdate, etc.., It currently only outputs, eg Closing 2 open 1 我遇到的问题是我需要从结果集中输出所有数据,例如标题,结束日期等。它目前仅输出,例如结束2打开1
really appreciate any help anyone can offer 非常感谢任何人都可以提供的帮助
-----------------------Updated------------------------------------- I think i have a working solution now, but its kind of messy - - - - - - - - - - - -更新 - - - - - - - - - - - - - -----------我想我现在有一个可行的解决方案,但是有点混乱
var groupdResults = from result in response.Results
orderby result[SearchSmartFormProperty.GetDateProperty("/root/FundClosingDate")]
group result by result[SearchSmartFormProperty.GetStringProperty("/root/DeadlineAltText")]
into statusGroup
select new
{
closingDate = statusGroup.Key,
count = statusGroup.Count(),
items = statusGroup.ToList()
};
List<Ektron.Cms.Search.SearchResultData> SRDATA = new List<Ektron.Cms.Search.SearchResultData>();
foreach (var result in groupdResults)
{
for (int i = 0; i < result.items.Count; i++)
{
SRDATA.Add(result.items[i]);
}
}
Any input as to a cleaner implementation? 关于更清洁的实现有什么意见吗? thanks
谢谢
You can do following: 您可以执行以下操作:
select new
{
closingDate =statusGroup.Key,
count= statusGroup.Count(),
items = statusGroup.ToList()
};
items
property will contain items that were assigned to that group in a List
. items
属性将包含在List
中分配给该组的项目。
Rather than using anonymous types I would put that into a new object. 与其使用匿名类型,不如将其放入一个新对象中。
var groupdResults =
from result in response.Results
orderby result[SearchSmartFormProperty.GetDateProperty("/root/FundClosingDate")]
group result by result[SearchSmartFormProperty.GetStringProperty("/root/DeadlineAltText")]
into statusGroup
select new ResultObject
{
closingDate = statusGroup.First().ClosingDate,
....
};
(Modified) (改性)
I think you need a class to capture your results: 我认为您需要一门课程来记录您的结果:
class GroupResult
{
public string Status { get; set; }
public ICollection<SearchResultData> Items { get; set; }
}
The grouping statusGroup
is an IEnumerable
of your items, so you can do: 分组
statusGroup
是您项目的IEnumerable
,因此您可以执行以下操作:
var groupdResults =
from result in response.Results
group result by result[SearchSmartFormProperty.GetStringProperty("/root/DeadlineAltText")]
into statusGroup
select new GroupResult
{
Status =statusGroup.Key,
Items = statusGroup.ToList()
}.ToList();
After that you can display the Items
in any way you wish, like sorted by Status
and the Items
sorted by ClosingDate
. 之后,你可以显示
Items
的需要,像分类以任何方式Status
和Items
排序ClosingDate
。
But maybe it is enough to just sort response.Results
by status and then by closing date. 但是也许仅按
response.Results
进行排序就足够了,结果按状态然后按截止日期排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.