在WP7上使用c#格式化LINQ SQL CE数据
本文关键字:LINQ SQL CE 数据 格式化 WP7 | 更新日期: 2023-09-27 18:08:34
我是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");
}
}
}
我需要格式化从数据库返回的数据,以下内容存储在我的DB中:
ItemDateTime - DateTime, ItemDescription - String, ItemAmount - Decimal
我需要能够根据用户的当前区域设置格式化日期,并将十进制格式设置为2 dp。
我也不确定我是否需要使用IQueryable当我得到数据结果。
任何帮助都将非常感激。
谢谢,div标记
既然你没有提供足够的细节-只是一个大致的想法
var boughtItemsInDB = from bought in BoughtItemDB.BoughtItems
select new { ItemDateTime = bought.ItemDateTime.ToString(), ItemDescription = bought.ItemDescription, ItemAmount = bought.ItemAmount.ToString("0,0.00") };
但是格式化最好在你用来显示数据的控件中完成,而不是在Linq查询中…
EDIT - after from OP:
从我看到的MoneySpent
类已经为"数据绑定"做好了准备…
所以格式化应该在显示控件中完成…有关一些信息,请参阅:
- 什么是WPF XAML数据绑定等效的String.Format?
- http://www.codeproject.com/KB/WPF/binding_in_linq-sql.aspx
- http://odetocode.com/code/740.aspx
- http://www.codeguru.com/csharp/.net/wp7/article.php/c18933