简体   繁体   English

来自数据表的C#LINQ查询

[英]C# LINQ query from datatable

I'm trying to implement a LINQ query on a search page we're working with. 我正在尝试在我们正在使用的搜索页面上实现LINQ查询。 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: 我已经将数据加载到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>();
}

and the SearchTag class: 和SearchTag类:

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. LINQ较新,但认为这应该更容易。 Thanks in advance. 提前致谢。

This will do the job: 这将完成工作:

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") })
        });

Then of course you have to map to your type... 然后,当然您必须映射到您的类型...

try something like the following, uses the GroupBy, Not sure what your SearchTags so replace x => new SearchTags(x) with how its constructed. 尝试类似以下的方法,使用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();

I am not clear how you want your output, based on which criteria, just ask if you want to be more precise. 我不清楚您希望根据哪种标准输出结果,只是问是否要更精确。

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;}
}

This should be fairly easy to do using GroupBy . 使用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()
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM