簡體   English   中英

在WP7上使用C#格式化LINQ SQL CE數據

[英]Formatting LINQ SQL CE data using C# on WP7

我是C#/ LINQ / WP7開發的新手,正在努力格式化從LINQ查詢返回的數據。

我具有以下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");
                }
            }
        }

我需要格式化從數據庫返回的數據,以下存儲在我的數據庫中:

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

我需要能夠根據用戶當前的語言環境來格式化日期,並將小數點格式化為2 dp。

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

任何幫助將非常感激。

謝謝馬克

由於您沒有提供足夠的細節-只是一個總體思路

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

BUT格式化最好在用於顯示數據的控件中完成,而不是在Linq查詢中...

編輯-在添加frm OP之后:

從我看來, MoneySpent類已經為“數據綁定”做好了准備...

因此,應在顯示控件中進行格式化...有關某些信息,請參閱:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM