在 WP7 上使用 C# 格式化 LINQ SQL CE 数据
Posted
技术标签:
【中文标题】在 WP7 上使用 C# 格式化 LINQ SQL CE 数据【英文标题】:Formatting LINQ SQL CE data using C# on WP7 【发布时间】:2011-09-13 20:17:13 【问题描述】:我是 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。
任何帮助将不胜感激。
谢谢, 标记
【问题讨论】:
谢谢...正如我所说:在显示控件中更好地进行格式化...添加了几个链接以帮助您开始...请参阅下面的答案... 【参考方案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 查询中...
EDIT - 添加frm OP后:
据我所知,MoneySpent
类已经为“数据绑定”做好了准备……
所以应该在显示控件中进行格式化...有关一些信息,请参阅:
What is the WPF XAML Data Binding equivalent of 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【讨论】:
感谢您的回复。我不知道我可以在控件中格式化结果,我在 Silverlight XAML 页面中使用结果。如果我可以在那里格式化,那我该怎么做? XAML 非常广泛(许多不同的控件),所以这不容易回答...您应该阅读将数据绑定到您要使用的控件,如果出现一些特定问题在这里提出一个新问题...请不要忘记投票/标记为已接受任何有帮助的答案... 我想我现在遇到了一个不同的问题,我尝试了你所说的但我的下一行现在失败了:'BoughtItems = new ObservableCollectionBoughtItems
是如何定义/声明的...通过在Linq 查询中进行格式化,创建了一个与DBControl.MoneySpent
不同的匿名类型。 .如果您显示DBControl.MoneySpent
的定义,也许还有其他可能的解决方案以上是关于在 WP7 上使用 C# 格式化 LINQ SQL CE 数据的主要内容,如果未能解决你的问题,请参考以下文章
WP7 Linq To SQL(SQL CE) IDataErrorInfo
在 WP7.1 SQL Server CE 数据库上执行 SQL 查询