LINQ查询和字符串数组 [英] LINQ query and Array of string

查看:157
本文介绍了LINQ查询和字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串的阵说:

String[] Fields=new String[]{RowField,RowField1}

在我可以使用下面的查询,通过指定值来获取值的查询,即 RowField RowField1

In which I can use the below query to get the values by specifying the values is query i.e RowField and RowField1:

var Result = (
    from x in _dataTable.AsEnumerable()
    select new
    {
        Name = x.Field<object>(RowField), 
        Name1 = x.Field<object>(RowField1)
    })
    .Distinct();

但如果假设我有很多值在阵列这样的:

String[] Fields= new String[]
{
    RowField,
    RowField1,
    RowField2,
    .......
    RowField1000
};

  • 如何在这里使用的查询,查询中不包含指定每个rowfield的?
  • 如何通过LINQ内部数组项循环?
  • 推荐答案

    从本质上讲,要检索从数据表的特定字段没有硬编码的字段名。

    Essentially, you want to retrieve specific fields from a DataTable without hardcoding the field names.

    下面code将每行返回一个字典对象,你在你的数组中指定的字段。有没有需要创建额外的扩展方法或comparers:

    The following code will return a single dictionary object per row with the fields you specify in your array. There is no need to create additional extension methods or comparers:

    var result = (from row in _dataTable.AsEnumerable()
                     let projection = from fieldName in fields
                          select new {Name = fieldName, Value = row[fieldName]}
                     select projection.ToDictionary(p=>p.Name,p=>p.Value));            
    

    内选择选秀权从每个表行,并将它们存储在投影变量所需的字段值。外选择转换这个变量的意思

    The inner select picks the field values you need from each table row and stores them in the projection variable. The outer select converts this variable in a Dictionary

    您可以遍历结果得到具体领域是这样的:

    You can iterate over the result to get specific fields like this:

    foreach (var row in result)
    {
        Console.WriteLine(row["field1"]);
    }
    

    编辑: 上述code不会返回不同的值。有可能返回不同的值,而无需使用的基由的编写一个专用的比较器,但code不是很pretty的:

    The above code doesn't return distinct values. It is possible to return distinct values without writing a special comparer using group by but the code is not very pretty:

    var result = (from row in table.AsEnumerable()
                    let projection = from fieldName in fields
                                    select new { Name = fieldName, Value = row[fieldName] }
                    group projection by projection.Aggregate((v, p) =>
                        new {
                            Name = v.Name + p.Name, 
                            Value = (object)String.Format("{0}{1}", v.Value, p.Value)
                        }) into g
                    select g.FirstOrDefault().ToDictionary(p=>p.Name,p=>p.Value));  
    

    ,总结创建了一个新的投影,其名称和V​​alue属性是所有名称和值字段的串联。聚集体的结果被用来组的所有行,并把每个组中的第一行。它的工作原理,但它肯定是丑陋的。

    The Aggregate creates a new projection whose Name and Value properties are the concatenation of all name and value fields. The result of the aggregate is used to group all rows and return the first row of each group. It works but it is definitely ugly.

    这将是最好创建一个简单的DictionaryComparer像下面的code:

    It would be better to create a simple DictionaryComparer like the following code:

        public class DictionaryComparer<TKey,TValue>: EqualityComparer<Dictionary<TKey,TValue>>
        {
            public override bool Equals(Dictionary<TKey, TValue> x, Dictionary<TKey, TValue> y)
            {
                //True if both sequences of KeyValuePair items are equal
                var sequenceEqual = x.SequenceEqual(y);
                return sequenceEqual;
            }
    
            public override int GetHashCode(Dictionary<TKey, TValue> obj)
            {
                //Quickly detect differences in size, defer to Equals for dictionaries 
                //with matching sizes
                return obj.Count;
            }
        }
    

    这允许你写的:

            var result = (from row in table.AsEnumerable()
                         let projection = from fieldName in fields
                                          select new {Name = fieldName, Value = row[fieldName]}                                                                                    
                         select projection.ToDictionary(p=>p.Name,p=>p.Value))
                         .Distinct(new DictionaryComparer<string, object>());
    

    这篇关于LINQ查询和字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆