2011-09-13 30 views
0

我對C#/ LINQ/WP7開發非常陌生,並且很努力格式化從我的LINQ查詢返回的數據。在WP7上使用C#格式化LINQ SQL CE數據

我有以下LINQ C#結構:

var boughtItemsInDB = from DBControl.MoneySpent bought in BoughtItemDB.BoughtItems 
select bought; 

BoughtItems = new ObservableCollection<DBControl.MoneySpent>(boughtItemsInDB); 

爲MoneySpent定義如下;

[Table(Name = "MoneySpent")] 
    public class MoneySpent : INotifyPropertyChanged, INotifyPropertyChanging 
    { 
     // Define ID: private field, public property and database column. 
     private int _itemId; 

     [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] 
     public int ItemId 
     { 
      get 
      { 
       return _itemId; 
      } 
      set 
      { 
       if (_itemId != value) 
       { 
        NotifyPropertyChanging("ItemId"); 
        _itemId = value; 
        NotifyPropertyChanged("ItemId"); 
       } 
      } 
     } 

     // Define item budget: private field, public property and database column. 
     private int _itemBudget; 

     [Column] 
     public int ItemBudget 
     { 
      get 
      { 
       return _itemBudget; 
      } 
      set 
      { 
       if (_itemBudget != value) 
       { 
        NotifyPropertyChanging("ItemBudget"); 
        _itemBudget = value; 
        NotifyPropertyChanged("ItemBudget"); 
       } 
      } 
     } 


     // Define item category: private field, public property and database column. 
     private string _itemCategory; 

     [Column] 
     public string ItemCategory 
     { 
      get 
      { 
       return _itemCategory; 
      } 
      set 
      { 
       if (_itemCategory != value) 
       { 
        NotifyPropertyChanging("ItemCategory"); 
        _itemCategory = value; 
        NotifyPropertyChanged("ItemCategory"); 
       } 
      } 
     } 



     // Define item description: private field, public property and database column. 
     private string _itemDescription; 

     [Column] 
     public string ItemDescription 
     { 
      get 
      { 
       return _itemDescription; 
      } 
      set 
      { 
       if (_itemDescription != value) 
       { 
        NotifyPropertyChanging("ItemDescription"); 
        _itemDescription = value; 
        NotifyPropertyChanged("ItemDescription"); 
       } 
      } 
     } 



     // Define item amount: private field, public property and database column. 
     private decimal _itemAmount; 

     [Column] 
     public decimal ItemAmount 
     { 
      get 
      { 
       return _itemAmount; 
      } 
      set 
      { 
       if (_itemAmount != value) 
       { 
        NotifyPropertyChanging("ItemAmount"); 
        _itemAmount = value; 
        NotifyPropertyChanged("ItemAmount"); 
       } 
      } 
     } 


     // Define item date: private field, public property and database column. 
     private DateTime _itemDateTime; 

     [Column] 
     public DateTime ItemDateTime 
     { 
      get 
      { 
       return _itemDateTime; 
      } 
      set 
      { 
       if (_itemDateTime != value) 
       { 
        NotifyPropertyChanging("ItemDateTime"); 
        _itemDateTime = value; 
        NotifyPropertyChanged("ItemDateTime"); 
       } 
      } 
     } 

我需要格式化從數據庫返回的數據,以下是保存在我的DB:

ItemDateTime - 日期時間,ItemDescription - 字符串,ItemAmount - 十進制

我需要能夠以根據用戶的當前區域設置格式化日期,並將小數格式設置爲2 dp。

我也不確定在獲取數據結果時是否需要使用IQueryable。

任何幫助將不勝感激。

謝謝, 馬克

+0

謝謝...正如我所說:格式化更好地完成顯示控制...添加了幾個鏈接,讓你開始在...看到我的答案在下面... – Yahia

回答

1

既然你沒有提供足夠的細節 - 只是你用來顯示數據的總體思路

var boughtItemsInDB = from bought in BoughtItemDB.BoughtItems 
select new { ItemDateTime = bought.ItemDateTime.ToString(), ItemDescription = bought.ItemDescription, ItemAmount = bought.ItemAmount.ToString("0,0.00") }; 

但格式的控制比較好做,而不是在LINQ查詢...

編輯 - 加FRM OP後:

從我看到的MoneySpent類已經爲「數據綁定」準備......

因此,格式化,應在顯示控制來實現......一些信息,請參閱:

+0

感謝您的答覆。我不知道我可以在控件中格式化結果,我在Silverlight XAML頁面中使用了結果。如果我可以在那裏格式化,那我該怎麼做? – MAO

+0

XAML是非常廣泛的(很多不同的控件),所以不容易回答...您應該閱讀關於要使用的控件的綁定數據,如果出現某個特定問題,請在此處提出一個新問題。 。請不要忘記註冊/標記爲已接受任何有幫助的答案... – Yahia

+0

我想我現在有一個不同的問題,我嘗試了你說的,但是我的下一行現在失敗了:'BoughtItems = new的ObservableCollection (boughtItemsInDB);」我是否需要使用IQueryable方法? – MAO