简体   繁体   中英

Using Inner Join in sqlite not returning the joined table's data

I have some medicines that I want to get their localized names for, but am having trouble getting their localized names (they are blank in the return).

Here's the query (at runtime).

SELECT m.Id as Id, m.Name as Name, lm.Name as ProductName 
FROM Medicine m 
INNER JOIN LocalizedMedicine lm 
    ON lm.MeId = m.Id 
    AND lm.LanguageCode = 'en-US' 
ORDER BY m.Name

Here are my classes.

public class LocalizedMedicine
{        
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [Indexed("LocMedName", 0)]
    public string Name { get; set; }
    public string LanguageCode { get; set; }
    public int MeId { get; set; }
}



public class Medicine : ObservableObject
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    /// <summary>
    /// The universal Latin medical name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// The primary Gene that this medicine uses.
    /// </summary>
    public int GeneId { get; set; }

    [Ignore]
    public Gene GeneUsed { get; set; }

    private string productName;
    /// <summary>
    /// The localized product name. It retrieves the
    /// localized name from AppResources if it is null.
    /// </summary>
    [Ignore]
    public string ProductName
    {
        get
        {
            // old attempt
            //if (productName == null)
            //    productName = AppResources.ResourceManager.GetString(Name);

            return productName;
        }
        set
        {
            productName = value;
            NotifyPropertyChanged("ProductName");
        }
    }

    /// <summary>
    /// Does a reference comparison and Name comparison, since the names (latin names)
    /// should be unique.
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    public override bool Equals(object m)
    {
        if (m == null) return false;
        if (m == this) return true;
        if (((Medicine)m).Name == this.Name) return true;
        return false;
    }
}

And here is how I make the call (in a PCL)

public IEnumerable<Medicine> GetAllMedicines(CultureInfo cultureInfo)
    {
        string langCode = cultureInfo.Name;

        // get all of the medicines and join LocalizedMedicine on MeId = med.Id
        // and LanguageCode = langCode


        var query = database.QueryAsync<Medicine>(
            "SELECT m.Id as Id, m.Name as Name, lm.Name as ProductName " +
            "FROM Medicine m " +
            "INNER JOIN LocalizedMedicine lm ON lm.MeId = m.Id AND lm.LanguageCode = " + "'" + langCode + "' " +
            "ORDER BY m.Name"
            );
        query.Wait();

        var result = query.Result;
        return result;
    }

Changed the query condition from "ON" to "WHERE" and removed the ignore attribute from the Medicine class. woops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM