how take details from a class - class

I have a method that gives me back all the films with a particular word inserted by user.
Now I want to copy all the details in one list so, when the user clicks a film that the app shows, it shows a toast with the corresponding ID.
How can i do this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace App_wrapper
{
public class Result1
{
public int vote_count { get; set; }
public int id { get; set; }
public bool video { get; set; }
public double vote_average { get; set; }
public string title { get; set; }
public double popularity { get; set; }
public string poster_path { get; set; }
public string original_language { get; set; }
public string original_title { get; set; }
public List<int> genre_ids { get; set; }
public string backdrop_path { get; set; }
public bool adult { get; set; }
public string overview { get; set; }
public string release_date { get; set; }
}
public class RootObject
{
public int page { get; set; }
public int total_results { get; set; }
public int total_pages { get; set; }
public List<Result1> results { get; set; }
}
}

Try this
Public List<Film> filmList;
public class Film
{
private String title;
private String id;
public Film(String title, String id)
{
this.title = title;
this.id = id;
}
public String getId()
{
return id;
}
public override string ToString()
{
return title;
}
}
foreach (var paolo in toor.results)
{
var fItem=new Film{title=paolo.title,id=paolo.id}
filmList.Add(fItem);
}
RunOnUiThread(() =>
{
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, filmList);
lv.Adapter = adapter;
});
//inside ListView_ItemClick
var id_film = filmList.ElementAt(e.Position).id;
For reference
how to use an ArrayAdapter in android of custom objects

Related

Failing to pass a complex object from one page to another in .Net Maui

I am trying to pass a complex object from MainPage to a ProductsPage, the object is a model with 4 class lists. Of the 4 class lists only 2 are passing data to the ProductsPage, the other 2 are not. I dont know where i am going wrong, i am using MVVM
My MainPageViewModel is as below
public partial class MainPageViewModel : BaseViewModel
{
public ObservableCollection<LogInModel> LogInModels { get; } = new();
public MainPageViewModel()
{
}
[ObservableProperty]
LogInModel logInModel;
[RelayCommand]
async Task GoToRetailAsync()
{
if (LogInModels.Count != 0)
LogInModels.Clear();
LogInModels.Add(logInModel);
await Shell.Current.GoToAsync($"{nameof(ProductsPage)}", true,
new Dictionary<string, object>
{
{"shiptoo",LogInModels[0].cat },
{"group",LogInModels[0].grp },
{"products",LogInModels[0].prod },
{"shipto",LogInModels[0].shp }
});
}
}
}
the failing class lists are shiptoo and group
Below is my ProductsViewModel
namespace Tenga.ViewModel
{
[QueryProperty("Products", "products")]
[QueryProperty("Group","group")]
[QueryProperty("Shiptoo","shiptoo")]
[QueryProperty("Shipto", "shipto")]
public partial class ProductsViewModel : BaseViewModel
{
public ProductsViewModel()
{
}
[ObservableProperty]
List<Shiptoo> shppp;
[ObservableProperty]
List<Group> groups;
[ObservableProperty]
List<Products> products;
[ObservableProperty]
List<Shipto> shipto;
}
}
Below is my LogInModel
namespace Tenga.Model
{
public class LogInModel
{
public string OTP { get; set; }
public string CustomerNumber { get; set; }
public string CustomerName { get; set; }
public string Balance { get; set; }
public string OpenToBuy { get; set; }
public string CreditLimit { get; set; }
public string LoginStatus { get; set; }
public string Error { get; set; }
public List<Shipto> shp = new List<Shipto>();
public List<Shiptoo> cat = new List<Shiptoo>();
public List<Group> grp = new List<Group>();
public List<Products> prod = new List<Products>();
}
public class Shipto
{
public string ShipCode { get; set; }
public string ShipDescription { get; set; }
}
public class Products
{
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public string UOM { get; set; }
public string ConversionFactor { get; set; }
public string Category { get; set; }
public string Group { get; set; }
public byte[] Image { get; set; }
}
public class Shiptoo
{
public string ShipCode { get; set; }
public byte[] Image { get; set; }
}
public class Group
{
public string Category { get; set; }
public string Code { get; set; }
public byte[] Image { get; set; }
}
}
I have tried to review the class all seems alright, i have also tried changing the bindings and result is the same, can some one please help before go crazy
Implement IQueryAttributable in your ViewModel.
And use:
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
Model = query[nameof(MyModel )] as MyModel ;
}
Forget about those annotations. This is better. You cant mistake names, you can run code after/before they are set. I migrated all my code to use this.
Edit: While we are on the subject:
Instead of:
{"shiptoo",LogInModels[0].cat },
You should be using some constants. The name of the model usually. (Something like naming conventions when passing Extras in android, but much more simple).

Returning Entity with its children

Hi I am trying to return all vehicles with their recorded mileage through an api using ASP.Net Core with the following code:
// GET: api/values
[HttpGet]
public IEnumerable<Vehicle> Get()
{
return _context.Vehicles.Include(m=>m.Mileages).ToList();
}
However this only returns the first vehicle with its mileages and not the others (there are five dummy vehicles in the db all with an initial mileage).
If I change the code to:
// GET: api/values
[HttpGet]
public IEnumerable<Vehicle> Get()
{
return _context.Vehicles.ToList();
}
it returns the full list of vehicles but no mileage.
My class files are:
public class Vehicle
{
public Vehicle()
{
Mileages = new List<Mileage>();
}
public int Id { get; set; }
public string Registration { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public Marked Marked { get; set; }
public ICollection<Mileage> Mileages { get; set; }
}
and
public class Mileage
{
public int Id { get; set; }
public DateTime MileageDate { get; set; }
public string RecordedMileage { get; set; }
//Navigation Properties
public int VehicleId { get; set; }
public Vehicle Vehicle { get; set; }
}
thanks for looking!
Tuppers
you can have them auto-load (lazy loading) using proxies... but for that, your foreign entities and collections must be marked virtual in your POCOs:
public class Mileage
{
public int Id { get; set; }
public DateTime MileageDate { get; set; }
public string RecordedMileage { get; set; }
//Navigation Properties
public int VehicleId { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class Vehicle
{
public Vehicle()
{
Mileages = new List<Mileage>();
}
public int Id { get; set; }
public string Registration { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public Marked Marked { get; set; }
public virtual ICollection<Mileage> Mileages { get; set; }
}
The proxy creation and lazy loading turned on, but that's the default in EF6.
https://msdn.microsoft.com/en-us/data/jj574232.aspx
Let me know if this works.
Well after a lot of searching I managed to find a solution. I used the following:
[HttpGet]
public IEnumerable<VehicleDto> Get()
{
var query = _context.Vehicles.Select(v => new VehicleDto
{
Registration = v.Registration,
Make = v.Make,
Model = v.Model,
Marked = v.Marked,
Mileages = v.Mileages.Select(m => new MileageDto
{
MileageDate = m.MileageDate,
RecordedMileage = m.RecordedMileage
})
.ToList(),
})
.ToList();
return (IEnumerable<VehicleDto>) query.AsEnumerable();
this doesn't seem to be the most elegant way of doing this, if anyone could offer any advice but it does return what is required.
The DTO's look like:
public class VehicleDto
{
public string Registration { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public Marked Marked { get; set; }
public ICollection<MileageDto> Mileages { get; set; }
}
and
public class MileageDto
{
public DateTime MileageDate { get; set; }
public string RecordedMileage { get; set; }
}
Thanks for taking the time to look at this
Tuppers

ViewModel To Model Use ExpressMapper List<object> to List<Model> as Field

My Model Is :
public class Product
{
public int id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int id { get; set; }
public string Name { get; set; }
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
}
And View Model Is :
public class ProductViewModel
{
public int id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public List<string> Tags { get; set; }
}
im using ExpressMapper To Mapping.
could it be map productviewModel List Tags To public ICollection Tags?
You can register your mappings like that:
Mapper.RegisterCustom<Tag, string>((tag) => tag.Name);
Mapper.Register<Product, ProductViewModel>();
Mapper.Compile();
Here is working example: https://dotnetfiddle.net/2r7l4z

Entity framework navigation property is null

I have two models using Entity Framework.
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Plays { get; set; }
public string FavouriteSurface { get; set; }
}
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public List<Player> Players { get; set; }
}
I am using the below code to attempt to display the Name of the player, based on the PlayerId in the SinglesMatch model matching the PlayerID from the Player model.
#foreach (var item in #Model)
{
<ul id="Players" class="bg-success"></ul>
<br/>
<h3>Date - #Html.DisplayFor(#modelItem => item.Date)</h3>
<li>Venue - #Html.DisplayFor(#modelItem => item.Venue)</li>
<li>Player 1 - #Html.DisplayFor(#modelItem => item.Players.First(p => p.PlayerId == item.Player1Id).Name)</li>
<li>Player 2 - #Html.DisplayFor(#modelItem => item.Players.First(p => p.PlayerId == item.Player2Id).Name)</li>
<li>Score- #Html.DisplayFor(#modelItem => item.Score)</li>
}
Upon debugging, the navigation property is always showing as null when the model is retrieved from my repository.
Am I using the navigation property in the correct fashion ? is there a problem with my query ?
Edit to include DbContext:
public TennisTrackerContext() : base("name=TennisTrackerContext")
{
}
public DbSet<Player> Players { get; set; }
public DbSet<PlayerRecord> PlayerRecords { get; set; }
public DbSet<SinglesMatch> SinglesMatches { get; set; }
public DbSet<DoublesMatch> DoublesMatches { get; set; }
public DbSet<Venue> Venues { get; set; }
}
}
You need to add a bridge table. Sql will create this automatically but you won't have access to the variables unless you create it in c#.
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Plays { get; set; }
public string FavouriteSurface { get; set; }
List<PlayerInMatch> Matches { get; set; }
public Player()
{
Matches = new List<PlayerInMatch>();
}
}
public class PlayerInMatch
{
public int Id { get; set; }
public int PlayerId { get; set; }
[ForeignKey("PlayerId")]
public Player Player { get; set; }
public int SinglesMatchId { get; set; }
[ForeignKey("SinglesMatchId")]
public SinglesMatch SinglesMatch { get; set; }
}
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public List<PlayerInMatch> Players { get; set; }
public SinglesMatch()
{
Players = new List<PlayerInMatch>();
}
}
static void Main(string[] args)
{
var match = new SinglesMatch();
match.Players.Select(c => c.Player.Name);
}
You need to make your navigation property virtual to enable lazy/eager loading:
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public virtual List<Player> Players { get; set; }
}
Also, did you define the relationship between SinglesMatch and Singles in fluent api?
EDIT: I see you don't have any relations mapped through annotations or fluent api whatsoever, I suggest you take a look at this:
https://msdn.microsoft.com/en-us/data/jj591617.aspx

Entity Framework New GUID database initializer

I want to initialize new GUID with my model class. I can not initialize GUID, Last Login IP adress.
Initializer doesn't create Kullanici table's row on database. Why ? I don't understand.
How can I add new row with GUID, IP Adress, DateTime.Now and more? Can you help me for that, please?
I'm using Entity Framework 6, MVC 5, SQL Server 2012, and ASP.NET 4.
Kullanici.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace agiEmlak.Entities
{
public class Kullanici
{
//public Kullanici()
//{
// GID = Guid.NewGuid();
//}
[Key]
public int ID { get; set; }
public Guid GID { get; set; }
public string KullaniciAdi { get; set; }
public string Ad { get; set; }
public string Soyad { get; set; }
public string Sifre { get; set; }
public string Eposta { get; set; }
public DateTime KayitTarihi { get; set; }
public string SonIPAdres { get; set; }
public DateTime SonLoginTarihi { get; set; }
public string AktivasyonKodu { get; set; }
public string CepTelefonu { get; set; }
public string IsTelefonu { get; set; }
public bool? Aktif { get; set; }
public bool? Silinmis { get; set; }
public string AdSoyad
{
get
{
return Ad + " " + Soyad;
}
}
public virtual ICollection<Emlak> Emlaks { get; set; }
}
}
ilanInitializer.cs:
using agiEmlak.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
namespace agiEmlak.Dal.Concrete.EntityFramework
{
public class ilanInitializer : DropCreateDatabaseIfModelChanges<ilanDBContext>
{
protected override void Seed(ilanDBContext context)
{
var kullanicilar = new List<Kullanici>
{
new Kullanici{ GID=Guid.NewGuid(), KullaniciAdi="halilkoca", Ad="Halil", Soyad="Koca", Eposta="halilkoca#outlook.com", Sifre="12551255", CepTelefonu="05415013031", IsTelefonu="02526133428", KayitTarihi=DateTime.Parse("25/10/2015"), AktivasyonKodu="5", SonIPAdres=Dns.GetHostName().ToString(), SonLoginTarihi= DateTime.Now},
new Kullanici{ GID=Guid.NewGuid(), KullaniciAdi="halil", Ad="Halil", Soyad="Koca", Eposta="halilkoca.ilan#gmail.com", Sifre="12551255", CepTelefonu="05415013031", IsTelefonu="02526133428", KayitTarihi=DateTime.Parse("26/10/2015"), AktivasyonKodu="10", SonIPAdres=Dns.GetHostName().ToString(), SonLoginTarihi= DateTime.Now}
};
kullanicilar.ForEach(s => context.Kullanicis.Add(s));
context.SaveChanges();
}
}
}
ilanDbContext.cs:
using System.Data.Entity;
using agiEmlak.Entities;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace agiEmlak.Dal.Concrete.EntityFramework
{
public class ilanDBContext:DbContext
{
public ilanDBContext(): base("ilanDBContext")
{
}
public DbSet<Adres> Adress { get; set; }
public DbSet<Ayar> Ayars { get; set; }
public DbSet<Detay> Detays { get; set; }
public DbSet<DetayTaksonomi> DetayTaksonomis { get; set; }
public DbSet<Emlak> Emlaks { get; set; }
public DbSet<EmlakKonutTuruMapping> EmlakKonutTuruMappings { get; set; }
public DbSet<EmlakTipi> EmlakTipis { get; set; }
public DbSet<EmlakTuru> EmlakTurus { get; set; }
public DbSet<Il> Ils { get; set; }
public DbSet<Ilce> Ilces { get; set; }
public DbSet<KonutTipi> KonutTipis { get; set; }
public DbSet<Kullanici> Kullanicis { get; set; }
public DbSet<Mahalle> Mahalles { get; set; }
public DbSet<Ozellik> Ozelliks { get; set; }
public DbSet<OzellikMapping> OzellikMappings { get; set; }
public DbSet<OzellikTaksonomi> OzellikTaksonomis { get; set; }
public DbSet<Ulke> Ulkes { get; set; }
/// <summary>
/// Database üzerine Tablo isimleri tekil olarak kaydedilir.
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
Solution:
Add Seed method execute to Global.asax.cs:
Database.SetInitializer<ilanDBContext>(new ilanInitializer());
Thanks.