2011-10-18 145 views
0

我需要查找数据库中每个字符串列的最大大小,作为设计另一个数据库的信息之一。我对源数据库的唯一访问权限是通过Web服务。我可以为每一列找到最大的尺寸,但我希望它是通用的,所以我可以稍后使用它。作为变量的属性名称

我写了这个非常简化的版本,使其易于理解。最后两行中有两句发明了语法,这是我需要帮助的地方。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    public class myClass 
    { 
     private string s; 
     public string S 
     { 
      get { return s; } 
      set { s = value; } 
     } 
     private int i; 
     public int I 
     { 
      get { return i; } 
      set { i = value; } 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Type myClassType = typeof(myClass); 
      System.Reflection.PropertyInfo[] propertyInfo = myClassType.GetProperties(); 
      Dictionary<string, int> property = new Dictionary<string, int>(); 

      foreach (System.Reflection.PropertyInfo info in propertyInfo) 
       if (info.PropertyType == typeof(System.String)) 
        property.Add(info.Name, -1); 

      myClass[] myPa = new myClass[2]; 
      myPa[0] = new myClass(); 
      myPa[0].S = "1"; 
      myPa[0].I = 0; 
      myPa[1] = new myClass(); 
      myPa[1].S = "12"; 
      myPa[1].I = 1; 

这是我需要帮助的地方。我发明了c[pair.key]。如何参考一个我知道名称的属性?

  foreach (myClass c in myPa) 
       foreach (KeyValuePair<string, int> pair in property) 
        if (c[pair.Key].Length > pair.Value) 
         property[pair.Key] = c[pair.Key].Length; 

      foreach (KeyValuePair<string, int> pair in property) 
       Console.WriteLine("Property: {0}, Biggest Size: {1}", pair.Key, pair.Value); 
     } 
    } 
} 

输出768,16是:

Property: S Biggest Size: 2 
+0

我不明白。 –

回答

2

以下就足够了:

static void Main() 
{ 
    // Demo data 
    myClass[] myPa = new myClass[2]; 
    myPa[0] = new myClass(); 
    myPa[0].S = "1"; 
    myPa[0].I = 0; 
    myPa[1] = new myClass(); 
    myPa[1].S = "12"; 
    myPa[1].I = 1; 

    PrintMaxLengthsOfStringProperties(myPa); 
} 

public static void PrintMaxLengthsOfStringProperties<T>(IEnumerable<T> items) 
{ 
    var t = typeof(T); 
    t.GetProperties().Where(p => p.PropertyType == typeof(string)) // TODO: Add other filters 
         .SelectMany(p => items.Select(o => (string)p.GetValue(o, null)).Select(v => new { Property = p, Value = v })) 
         .GroupBy(u => u.Property) 
         .Select(gu => new { Property = gu.Key, MaxLength = gu.Max(u => u.Value != null ? u.Value.Length : 0) }) 
         .ToList() 
         .ForEach(u2 => Console.WriteLine("Property: {0}, Biggest Size: {1}", u2.Property.Name, u2.MaxLength)) 
         ; 
} 

虽然你可能会添加上“的GetProperties”结果集一些额外的过滤器(如取出静态的,或者索引属性等

它使使用几个Linq扩展函数,即“Where”,“GroupBy”,“SelectMany”,“Select”和“Max”以及使用匿名类型。

+0

这个作品,我接受了。但我会坚持一个解决方案,而不会依赖Linq,尽管我怀疑没有一个。 –

+0

你完全可以在没有LINQ的情况下编写它。它会花更长的时间... – Reddog

0

还不是很确定,如果我的理解没错,但:

如果只使用索引属性在类这是什么[PROPERTYNAME]这里面将返回指定属性的值,如对象。

public class myClass 
    { 
     .... 
     .... 
     public object this[string propertyname] 
     { 
      get { /*use reflection to return property value, so Object*/ } 

     } 
     .... 
     .... 
    } 

这意味着,我很害怕,你不能只是写c[pair.key].Length,为Object没有Length财产。您需要将其转换为所需的类型(您的案例中为string),并且仅在使用Length属性后。

如果这不是你要求的,请重新填写你的问题。

+0

那个类是只是一种简化,我无法控制从哪个eac的真实课程由Web服务返回的h行是一个实例。 –