2017-04-24 24 views
0

我是Xamarin表單中的新成員。我創建了兩個表&試圖對這兩個表執行聯合操作,但我無法加入表。在Xamarin表單應用中的SQLite中加入操作

我已經通過這些鏈接已經走了,但沒有成功

Link1Link2

誰能幫助我如何加入Xamarin形式兩個表。以下是我的JOIN操作代碼

public Thoughts GetQueryForAllRecords(int ID) 
{ 
    var q = connection.Query<Thoughts>("SELECT Thought,CreatedOn, AssignedToName, AssignedOn FROM Thoughts JOIN AssignedThought ON Thoughts.ID = AssignedThought.ID where Thoughts.ID=?;", ID); 

    return q.Last(t => t.ID == ID); 
} 

但上面的代碼只返回'想法'表recods不是從'AssignedThought'表。

請建議我在這裏做錯了什麼。

回答

1

我覺得你可以看看到sqlite-net-extensions

SQLite的-Net的擴展是一個非常簡單的ORM,提供一個對一個,一到多,多到一個,許多-TO- sqlite-net庫之上的許多反轉和文本blobbed關係。 sqlite-net是一個開源的,最小的庫,允許.NET和Mono應用程序在SQLite 3數據庫中存儲數據。 SQLite-Net擴展擴展了它的funcionality,以幫助用戶處理sqlite-net實體之間的關係。

public class Stock 
    { 
     [PrimaryKey, AutoIncrement] 
     public int Id { get; set; } 
     [MaxLength(8)] 
     public string Symbol { get; set; } 

     [OneToMany(CascadeOperations = CascadeOperation.All)]  // One to many relationship with Valuation 
     public List<Valuation> Valuations { get; set; } 
    } 


     public class Valuation 
     { 
      [PrimaryKey, AutoIncrement] 
      public int Id { get; set; } 

      [ForeignKey(typeof(Stock))]  // Specify the foreign key 
      public int StockId { get; set; } 
      public DateTime Time { get; set; } 
      public decimal Price { get; set; } 

      [ManyToOne]  // Many to one relationship with Stock 
      public Stock Stock { get; set; } 
     } 

下面是我們如何將創建,讀取和更新實體:

var db = Utils.CreateConnection(); 
db.CreateTable<Stock>(); 
db.CreateTable<Valuation>(); 

var euro = new Stock() { 
    Symbol = "€" 
}; 
db.Insert(euro); // Insert the object in the database 

var valuation = new Valuation() { 
    Price = 15, 
    Time = DateTime.Now, 
}; 
db.Insert(valuation); // Insert the object in the database 

// Objects created, let's stablish the relationship 
euro.Valuations = new List<Valuation> { valuation }; 

db.UpdateWithChildren(euro); // Update the changes into the database 
if (valuation.Stock == euro) { 
    Debug.WriteLine("Inverse relationship already set, yay!"); 
} 

// Get the object and the relationships 
var storedValuation = db.GetWithChildren<Valuation>(valuation.Id); 
if (euro.Symbol.Equals(storedValuation.Stock.Symbol)) { 
    Debug.WriteLine("Object and relationships loaded correctly!"); 
}