我第一次在手机上玩Linq-To-Sql。我已经创建了一个简单的类,但除了基本的选择查询之外的任何东西,即“从db.Icons中选择p的p'都会引发异常。
var q = from p in db.Icons
where p.Name == "testa"
select p;
// ^ Throws 'The member Icon.Name has no supported translation to SQL'
除了下面粘贴的源代码中的异常原因外,我还有2个问题:
[Column]
private bool _isFavourite = false; // Does this actually set a default value?
// Should I use Nullable<T> for value types?
// i.e. should this be 'bool?' instead.
-
public Table<Icon> Icons
{
get // Is this getter necessary? Wouldn't 'Table<Icon> Icons' suffice?
{
return this.GetTable<Icon>();
}
}
来源:
//Icon.cs
[Table]
public class Icon : INotifyPropertyChanged, INotifyPropertyChanging
{
private const string IdPropertyName = "Id";
private const string NamePropertyName = "Name";
private const string IsFavouritePropertyName = "IsFavourite";
private const string IconUrlPropertyName = "IconUrl";
[Column(IsPrimaryKey=true, IsDbGenerated=true)]
private int _id;
[Column]
private string _name;
[Column]
private bool _isFavourite = false; // Does this actually set a default value?
// Should I use Nullable<T> for value types?
// i.e. should this be 'bool?' instead.
[Column]
private string _iconUrl;
public int Id
{
get
{
return _id;
}
set
{
RaisePropertyChanging(IdPropertyName);
_id = value;
RaisePropertyChanged(IdPropertyName);
}
}
public string Name
{
get
{
return _name;
}
set
{
RaisePropertyChanging(NamePropertyName);
_name = value;
RaisePropertyChanged(NamePropertyName);
}
}
public bool IsFavourite
{
get
{
return _isFavourite;
}
set
{
RaisePropertyChanging(IsFavouritePropertyName);
_isFavourite = value;
RaisePropertyChanged(IsFavouritePropertyName);
}
}
public string IconUrl
{
get
{
return _iconUrl;
}
set
{
RaisePropertyChanging(IconUrlPropertyName);
_iconUrl = value;
RaisePropertyChanged(IconUrlPropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangingEventHandler PropertyChanging;
public void RaisePropertyChanging(string propertyName)
{
PropertyChangingEventHandler handler = PropertyChanging;
if (handler != null)
{
handler(this, new PropertyChangingEventArgs(propertyName));
}
}
}
-
//IconsManagerContext.cs
public class IconsManagerContext : DataContext
{
public Table<Icon> Icons
{
get // Is this getter necessary? Wouldn't 'Table<Icon> Icons' suffice?
{
return this.GetTable<Icon>();
}
}
private const string DbConnectionString = @"DataSource=isostore:/IconsManager.sdf";
public IconsManagerContext()
: this(DbConnectionString)
{
}
public IconsManagerContext(string connectionString)
: base(connectionString)
{
if (!DatabaseExists())
{
CreateDatabase();
Icons.InsertAllOnSubmit<Icon>(new List<Icon>()
{
new Icon() {
IconUrl="/Images/testa.jpg",
Name="Testa",
IsFavourite=true
},
new Icon() {
IconUrl="/Images/testb.jpg",
Name="Testb",
}
this.SubmitChanges();
}
}
}
-
//MainPage.xaml
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
using (var db = new IconsManagerContext())
{
var q = from p in db.Icons
where p.Name == "testa"
select p;
// ^ Throws 'The member Icon.Name has no supported translation to SQL'
IconsListBox.ItemsSource = q;
};
}
}
答案 0 :(得分:1)
您没有名为Name
且标记为[Column]
的属性,因此您不应对获得异常感到惊讶。使用[Column]
而非字段标记您的属性,它应该有效。
您应该阅读WP7 SQL CE系列:http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database(SQL-CE)-Introduction