我正在尝试在我们正在使用的搜索页面上实现LINQ查询。从初始查询返回的数据布局如下:
ListID ListName TagId TagValue
1 Name1 1 Tag1
1 Name1 2 Tag2
2 Name2 3 Tag3
基本上,列表是搜索类别,标签是搜索词。我要做的是恢复此列表,将其分成唯一列表,然后在页面上显示相关标签。
我将数据加载到dataTable中,现在我正在尝试将列表名称放入我的SearchCategories对象中:
public class SearchCategories
{
public int SearchCategoryId { get; set; }
public string SearchCategoryName { get; set; }
public SearchTags searchTags { get; set; }
public List<SearchTags> lstSearchTags= new List<SearchTags>();
}
和SearchTag类:
public class SearchTags
{
public int SearchTagId { get; set; }
public string SearchTagValue { get; set; }
}
然后将标签放入对象的相关列表中。
这是我正在使用的代码:
dtLists.Load(reader);
var distinctValues = dtLists.AsEnumerable()
.Select(row => new SearchCategories
{
SearchCategoryId = row.Field<int>("ListNameID"),
SearchCategoryName = row.Field<string>("listName")
})
.Distinct();
LINQ更新,但认为这应该更容易。提前谢谢。
答案 0 :(得分:2)
这将完成这项工作:
var result = dtLists.AsEnumerable()
.GroupBy(m => new
{
ListID = m.Field<int>("ListID"),
ListName = m.Field<string>("ListName")
}, m => m)
.Select(m => new
{
m.Key.ListID,
m.Key.ListName,
Tags = m.Select(x => new { x.Field<int>("TagId"), x.Field<string>("TagValue") })
});
当然,你必须映射到你的类型......
答案 1 :(得分:1)
尝试类似以下内容,使用GroupBy,不确定您的SearchTags
是如何将x => new SearchTags(x)
替换为其构造方式。
dtLists.AsEnumerable()
.Select(row => new {
SearchCategoryId = row.Field<int>("ListID "),
SearchCategoryName = row.Field<string>("listName"),
TagId = row.Field<int>("TagId"),
TagValue = row.Field<string>("TagValue")
})
.GroupBy(x => x.SearchCategoryId)
.Select(g => new SearchCategories() {
SearchCategoryId = g.Key,
SearchCategoryName = g.First().SearchCategoryName,
lstSubCategories = g.Select(x => new SearchTags(){ SearchTagId = x.TagId, SearchTagValue = x.TagValue}).ToList()
}).ToList();
答案 2 :(得分:1)
根据具体标准,我不清楚您希望输出的方式,只是询问您是否想要更精确。
void Main()
{
var list = new List<Data>(){
new Data
{
ListId = 1,
ListName = "Name1",
TagId = 1,
TagValue = "Tag1"
},
new Data
{
ListId = 1,
ListName = "Name1",
TagId = 2,
TagValue = "Tag2"
},
new Data
{
ListId = 2,
ListName = "Name2",
TagId = 3,
TagValue = "Tag3"
}};
//this is to group all SearchCategories by SearchCategoryName
var res = list.Select(x => new SearchCategories
{
SearchCategoryId = x.ListId,
SearchCategoryName = x.ListName
}).GroupBy(x => x.SearchCategoryName);
//to have just distinct categories one time
var res2 = list.Select(x => new SearchCategories
{
SearchCategoryId = x.ListId,
SearchCategoryName = x.ListName
}).Distinct(new SearchCategoriesComparer());
//display your data here based on a Key. The output should be Key=Name1 with two elements inside and the other one with one element Key=Name2
res.Dump();
//display your data here
res2.Dump();
}
public class Data
{
public int ListId {get; set;}
public string ListName { get; set; }
public int TagId { get; set; }
public string TagValue { get; set;}
}
public class SearchCategories
{
public int SearchCategoryId { get; set; }
public string SearchCategoryName { get; set; }
public List<SearchTags> lstSubCategories = new List<SearchTags>();
}
public class SearchCategoriesComparer : IEqualityComparer<SearchCategories>
{
public bool Equals(SearchCategories x, SearchCategories y)
{
return x.SearchCategoryName == y.SearchCategoryName;
}
public int GetHashCode(SearchCategories obj)
{
return obj.SearchCategoryName.GetHashCode();
}
}
public class SearchTags
{
public string Tag{ get; set;}
}
答案 3 :(得分:1)
使用GroupBy
,这应该相当容易。
dtLists.AsEnumerable()
.GroupBy(x => new { ListNameID = x.Field<int>("ListNameID"), ListName = x.Field<string>("ListName") })
.Select(x => new SearchCategories()
{
SearchCategoryId = x.Key.ListNameID,
SearchCategoryName = x.Key.ListName,
lstSearchTags = x.Select(y => new SearchTags(){ SearchTagId = y.Field<int>("TagId"), SearchTagValue = y.Field<string>("TagValue") }).ToList()
});