2

I have created a Xamarin.Forms app in Visual Studio that connects to an Azure SQL Database via an Azure Mobile App. So far everything is working fine - I can do CRUD operations on the database from my Xamarin App.

However I have not yet been able to perform an inner join query on two or more tables.

I have created one such query in a View on the Database, and it works ok on the server, but my problem is that I do not know how to connect to this View from the Xamarin.Forms app.

I would really appreciate any help :-)

Thank you!

XAMARIN.FORMS SERVICE CLASS FOR A GENERIC AZURE TABLE:

public class service<T> : serviceBase<T> where T : irlm.azure.models.modelBase
{
    public async Task<ObservableCollection<T>> get()
    {
        var items = await table.ToEnumerableAsync();
        return new ObservableCollection<T>(items);
    }

    public async Task<T> update(T item)
    {
        if (item.Id == null)
            await table.InsertAsync(item);
        else
            await table.UpdateAsync(item);
        return item;
    }

    public async Task delete(T item)
    {
        await table.DeleteAsync(item);
    }
}

public abstract class serviceBase<T>
{
    MobileServiceClient dc;
    public IMobileServiceTable<T> table;

    public serviceBase()
    {
        dc = new MobileServiceClient(irlm.constants.appUrl);
        table = dc.GetTable<T>();
    }
}

XAMARIN.FORMS MODEL EXAMPLE

public abstract class modelBase
{
    public string Id { get; set; }
}

public class golfClub : modelBase
{
    public string name { get; set; }
}

public class golfCourse : modelBase
{
    public string idClub { get; set; }
    public string name { get; set; }
}

XAMARIN.FORMS VIEWMODEL:

public class viewModel<T> : INotifyPropertyChanged
{
    private ObservableCollection<T> _items = new ObservableCollection<T>();
    public ObservableCollection<T> items
    {
        get { return _items; }
        set
        {
            _items = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

CONTENT PAGE:

irlm.azure.viewModels.viewModel<irlm.azure.models.golfClub> golfClubs = new azure.viewModels.viewModel<azure.models.golfClub>();
irlm.azure.services.service<irlm.azure.models.golfClub> dc = new azure.services.service<azure.models.golfClub>();
ListView list;
Entry txt = new Entry();

private async void BtnAdd_Clicked(object sender, EventArgs e)
{
   var golfCourse = await dc.update(new irlm.azure.models.golfClub { name = txt.Text });
   golfClubs.items.Add(golfCourse);
}

private async void BtnUpdate_Clicked(object sender, EventArgs e)
{
    var index = golfClubs.items.IndexOf(list.SelectedItem as irlm.azure.models.golfClub);
    var item = (irlm.azure.models.golfClub)list.SelectedItem;
    item.name = txt.Text;
    await dc.update(item);
    golfClubs.items[index] = item;
}

private async void BtnRemove_Clicked(object sender, EventArgs e)
{
    var item = (irlm.azure.models.golfClub)list.SelectedItem;
    await dc.delete(item);
    golfClubs.items.Remove((irlm.azure.models.golfClub)list.SelectedItem);
    txt.Text = "";
}
Ian Moore
  • 21
  • 4
  • Show us your code – Jeroen Heier Dec 18 '16 at 12:18
  • Hi Jeroen, here is my View in the SQL db: – Ian Moore Dec 18 '16 at 14:02
  • CREATE VIEW [dbo].[golfClub_golfCourse] AS SELECT [golfCourses].Id, club=[golfClubs].name, course=[golfCourses].name FROM [golfCourses] inner join [golfClubs] on [golfClubs].Id=[golfCourses].idClub – Ian Moore Dec 18 '16 at 14:03
  • public class service : serviceBase where T : irlm.azure.models.modelBase { public async Task> get() { var items = await table.ToEnumerableAsync(); return new ObservableCollection(items); } public async Task update(T item) { if (item.Id == null) await table.InsertAsync(item); else await table.UpdateAsync(item); return item; } } – Ian Moore Dec 18 '16 at 14:06
  • sorry, I am not formatting this code at all. It's my first time on one of these forums :-( – Ian Moore Dec 18 '16 at 14:07
  • Dear @jeroenheier, I have added some code now. If you need more please let me know. Thanks for your support :-) – Ian Moore Dec 19 '16 at 15:53
  • Dear @JeroenHeier, Just wanted to check if the code that I uploaded to the original question was what you needed?Please let me know if you need more. – Ian Moore Jan 09 '17 at 09:34
  • you have given a background for your question but still not very sure where you have a problem. Do you want to create a join between golfClub and golfCourse? See also [this](http://stackoverflow.com/questions/39095064/writing-a-nested-join-with-linq-to-entities) SO question. – Jeroen Heier Jan 09 '17 at 17:37
  • Hi Jeroen, thanks for your reply. Yes, I want to create a join query on these two tables from Xamarin.I have not been able to find any information on this anywhere. – Ian Moore Jan 09 '17 at 21:24
  • The Azure Mobile Apps creates table controllers which I can only manipulate individually so far. Can't seem to see how to combine two (or more) tables in one query. – Ian Moore Jan 09 '17 at 21:25
  • The only info that I could find was that you could use a View on the Azure SQL db, which I have created successfully. But I just have not b een able to understand how to connect to it from Xamarin. Hope I am explaining this ok? – Ian Moore Jan 09 '17 at 21:27
  • I found [this](https://www.simple-talk.com/cloud/cloud-data/t-sql-sql-data-warehouse) article for some background. – Jeroen Heier Jan 10 '17 at 21:32
  • Thanks Jeroen! Unfortunately I don't believe that one can use TSQL from Xamarin to query Azure Db Tables. I realize that my understanding is far from complete, but it seems that the most convenient method is to do multi-table queries via SQL DB Views and a corresponding table controller in an ASP Web App. If I can create the table controller for the View then I can connect to it easily from Xamarin. Thanks again! – Ian Moore Jan 11 '17 at 19:18

0 Answers0