I'm trying to implement a LINQ query on a search page we're working with. The data coming back from the initial query is laid out as follows:
ListID ListName TagId TagValue
1 Name1 1 Tag1
1 Name1 2 Tag2
2 Name2 3 Tag3
Basically the lists are search categories and the tags are search terms. What I'm trying to do is bring back this list, separate it into unique lists, and then display the related tags on the page.
I have the data loaded into a dataTable, now I'm trying to get the list names into my SearchCategories object:
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>();
}
and the SearchTag class:
public class SearchTags
{
public int SearchTagId { get; set; }
public string SearchTagValue { get; set; }
}
and then the tags into the object's related list.
Here's the code I'm working with:
dtLists.Load(reader);
var distinctValues = dtLists.AsEnumerable()
.Select(row => new SearchCategories
{
SearchCategoryId = row.Field<int>("ListNameID"),
SearchCategoryName = row.Field<string>("listName")
})
.Distinct();
Newer to LINQ but think this should be easier. Thanks in advance.
GroupBy(instead ofDistinct).