5
I’m creating an app on Xamarin, which generates a sales order, and stores on mobile.
I’m using Sqlite to store the data, but I’m having trouble making a Join between my Order Items table, and the Products table.
I have the two models below:
[Table("tbPROD")]
public class PROD
{
[PrimaryKey, AutoIncrement]
public int PROD_Id { get; set; }
[MaxLength(100)]
public string PROD_Descricao { get; set; }
public decimal PROD_Preco { get; set; }
}
[Table("tbITPD")]
public class ITPD
{
[PrimaryKey]
public int ITPD_Id { get; set; }
public int ITPD_PEDI_Id { get; set; }
public int ITPD_PROD_Id { get; set; }
public int ITPD_Quantidade { get; set; }
public decimal ITPD_ValorTotal { get; set; }
public override string ToString()
{
return string.Format("{0} {1} {2} {3}", ITPD_Id, ITPD_PROD_Id, ITPD_Quantidade, ITPD_ValorTotal);
}
}
I needed to make a select on the ITPD by taking the product description, in the case of Inner Join tbPROD on Prod_id = Itpd_prod_id. But in Sqlite I’m not able to do this, something else too, I can do select mount in Sqlite? Type, "select Prod_id, Prod_descricao from tbPROD".
I’m asking this, because the selects I’m using, this way:
public List<PROD> GetProdutos(string valor)
{
List<PROD> list = new List<PROD>();
try
{
list = valor == string.Empty ?
_SQLiteConnection.Table<PROD>()
.OrderBy(p => p.PROD_Id)
.ToList() :
_SQLiteConnection.Table<PROD>()
.Where(p => p.PROD_Descricao.Contains(valor))
.OrderBy(p => p.PROD_Id)
.ToList();
}
catch (Exception ex)
{
HasError = true;
MsgError = ex.Message;
}
return list;
}
@Randrade, I couldn’t follow with the link you gave me, actually, I’m not getting to understand how it works.
Could someone make an example for me, with these two classes of mine?
Goes below:
With the Pedi_clie_description field in the PEDI class, my select works correctly! But what would it look like if I took this field? What modeling and select would look like?
Yeah, I got it! But in Query<PROD>, I cannot put my PROD class there, because it will return fields of the ITPD and PROD class, then it would not work. I downloaded this extension, but I couldn’t use it right!
– felipedrt
@felipedrt I put there just as example. You should put the correct class.
– Randrade
So that’s the problem! You don’t have the right class, I’m trying to use Dynamic to see if I can do something.
– felipedrt
@felipedrt The way I put it, you are working with dynamic data. The
Xamarin
does this conversion automatically. But if you don’t have a class to put in the query, there is something very wrong with your modeling.– Randrade
So actually that’s what’s going on, there’s something wrong with the actual modeling. What should I do, create a class, with the data that will be returned? Or put the fields in the ITPD class? Need to return these fields in my select -> Item id, Order id, Product description, Product value, Quantity, Total value; Product description, and value, are in the PROD class, the other fields are in the ITPD class..
– felipedrt
@felipedrt You don’t need to create a new class, you just need to do a correct modeling, using
OneToOne
orOneToMany
, depending on your context. Look at this example, I think it will help you.– Randrade
All right, I’ll see if I can model right here, thanks!
– felipedrt