2012-03-19 122 views
1

我在手机上首次使用Linq-To-Sql。我创建了一个简单的类,但除了基本的选择查询之外,即“从db.Icons中的p选择p”引发异常。简单的linq-to-sql查询异常

 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; 
     }; 
    } 
} 

回答