Xamarin - Sqlite Join

Asked

Viewed 610 times

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:

CLIE (Customers): inserir a descrição da imagem aqui

PEDI (Requests) inserir a descrição da imagem aqui

Below is the select: inserir a descrição da imagem aqui

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?

1 answer

1

Sqlite does not support .Join via linq of course. What you can do is make the consultation in hand.

_SQLiteConnection.Query<PROD>(
        "select * from tbPROD p INNER JOIN tbITPD pd on p.PROD_Id = pd.ITPD_PROD_id").ToList();

More details can be seen on official documentation.

Now, if you want to use a third-party library, there is a Sqlite . Net Extensions, which makes it possible to use the .Join via linq.

  • 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 I put there just as example. You should put the correct class.

  • 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 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.

  • 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 You don’t need to create a new class, you just need to do a correct modeling, using OneToOne or OneToMany, depending on your context. Look at this example, I think it will help you.

  • All right, I’ll see if I can model right here, thanks!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.