2017-05-16 7 views
1

これは私の最初の質問です。間違いを無視してください。 私は、 "urlAll"という名前の文字列に5つの属性、5つの属性を渡すC#プロジェクトを実行しています。前に宣言した*#06#でプロパティを分割するつもりです。windowsフォームアプリケーションリストからADO.NETデータテーブル

public DataTable GetTwitterDataTableMulti(string urlAll) 
    { 

     Trace.bDebug = false; 
     if (ExitNow) 
     { 
      return null; 
     } 

     string[] allurl = urlAll.Split(new string[] { "*#06#" }, StringSplitOptions.None); 

    return Dt; 
    } 

LinqToTwitterを使用してWindowsフォームアプリケーションで別のプロジェクトを作成し、リストを返します。私が必要とするのは、前に書いたado.netテーブルのようにリストを返すことだけです。以下は私のtwitterフィードコードです。

//initializing a list of current news feeds 
    private List<Status> currentTweets; 

    //parent class of the form application 
    //interface 
    public TwitterFeed() 
    { 
     //initializing the components 
     InitializeComponent(); 
     //call the function that will display current 200 feeds from twitter 
     GetMostRecent200HomeTimeLine(); 
     //clearing the textbox in which the feeds will be displayed 
     lstTweetList.Items.Clear(); 

     //displaying the feed in text box right 
     currentTweets.ForEach(tweet => lstTweetList.Items.Add(tweet.User.Name + ":" + tweet.Text)); 
     //display follow list in text box left 
     //GetSideBarList(GetFollowers()).ForEach(name => lstFollowNames.Items.Add(name)); 

    } 


    //method that gets current 200 feeds form twitter of the user 
    private void GetMostRecent200HomeTimeLine() 
    { 
     var twitterContext = new TwitterContext(authorizer); 
     var tweets = from tweet in twitterContext.Status 
        where tweet.Type == StatusType.Home && 
        tweet.Count == 200 
        select tweet; 
     currentTweets = tweets.ToList(); 
    } 

どのような方法で統合するのですか?あなたのリストは、他のコレクションがない場合、これは動作するはず

+0

を、私は、データベースに挿入します。 –

+0

共有した2つのコードスニペットが互いにどのように関係していますか? 'Status'クラスの様子は? 2番目のコードスニペットでDataTableを返すメソッドはどこですか? –

+0

と関連しています。私は最初のコード内で2番目のコードを使いたいからです。 2番目のコードにはクラスとインタフェースがありますので、別のクラスの中でクラスを使用することはできません。 私の主張は、LinqToTwitterライブラリ(Twitter API)を使用することです。 ステータスはLinqToTwitterのデフォルト/ビルドクラスです。私はリストを返すWindowsフォームアプリケーションを使ってツイッターからライブフィードを収集することができ、リストボックスを通して表示できました。 今、リストの代わりにDataTableを返すC#クラスライブラリプロジェクトにコードを実装したいと思います。 –

答えて

0

:私はC++のDataTableにC#のDataTableを渡す必要があるため

public static DataTable ToDataTable<T>(this IList<T> list) 
     { 
     DataTable table = null; 
     if (list != null && list.Count > 0) 
     { 
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); 

      List<PropertyDescriptor[]> propList = new List<PropertyDescriptor[]>(); 

      table = new DataTable(); 
      foreach (PropertyDescriptor item in properties) 
      { 
       if (IsArrayOrCollection(item.PropertyType)) 
       { 
        continue; 
       } 
       AddColumnsForProperties(typeof(T).Namespace, null, table, (new[] { item }).ToList(), ref propList); 
      } 

      object[] values = new object[propList.Count]; 

      foreach (T item in list) 
      { 
       for (int i = 0; i < values.Length; i++) 
        values[i] = GetValueFromProps(propList[i], item) ?? DBNull.Value; 

       table.Rows.Add(values); 
      } 
     } 
     return table; 
     } 

    private static object GetValueFromProps(PropertyDescriptor[] descriptors, object item) 
    { 
    var result = item; 
    foreach (var descriptor in descriptors) 
    { 
     if (result != null) 
     { 
      result = descriptor.GetValue(result); 
     } 
    } 
    return result; 
    } 


    private static void AddColumnsForProperties(string myNamespace, string parentName, DataTable dt, List<PropertyDescriptor> p, ref List<PropertyDescriptor[]> properties) 
    { 
    var pLast = p.Last(); 

    if (pLast.PropertyType.Namespace != null && pLast.PropertyType.Namespace.StartsWith(myNamespace)) 
    { 
     var allProperties = pLast.GetChildProperties(); 
     if (allProperties.Count > 0) 
     { 
      foreach (PropertyDescriptor item in allProperties) 
      { 
       var newP = p.ToList(); 
       newP.Add(item); 

       string childParentName = !String.IsNullOrEmpty(parentName) 
       ? String.Format("{0}.{1}.{2}", parentName, pLast.Name, item.Name) 
       : String.Format("{0}.{1}", pLast.Name, item.Name); 
       if (item.PropertyType.Namespace != null && item.PropertyType.Namespace.ToLower().StartsWith(myNamespace)) 
       { 
       AddColumnsForProperties(myNamespace, childParentName, dt, newP, ref properties); 
       } 
       else if (!dt.Columns.Contains(childParentName)) 
       { 
       dt.Columns.Add(childParentName, Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType); 
       properties.Add(newP.ToArray()); 
       } 
      } 
     } 
     else if (!dt.Columns.Contains(pLast.Name)) 
     { 
      dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType); 
      properties.Add(p.ToArray()); 
     } 
    } 
    else if (!dt.Columns.Contains(pLast.Name)) 
    { 
     dt.Columns.Add(pLast.Name, Nullable.GetUnderlyingType(pLast.PropertyType) ?? pLast.PropertyType); 
     properties.Add(p.ToArray()); 
    } 
    } 
+0

ありがとうございます。私はこれを試すつもりです。お知らせいたします。 –

関連する問題