I'm having an issue trying to convert an Enum to JSON string for jQGrid. The format I was using before (doing a manually conversion) was this:
{{0: '-', 1: 'Active', 2: 'Deactive', 3: 'Pending'}}
public static string GetStatuses(bool addDefault = false)
{
var statusesEnum = Enum.GetValues(typeof(StatusEnum));
string statuses = "{value: {0: '-', ";
foreach (StatusEnum status in statusesEnum)
statuses += String.Format("{0}: '{1}', ", (byte)status, Enum.GetName(typeof(StatusEnum), status));
return statuses.Substring(0, statuses.Length - 2) + "}}";
}
So I need to avoid this method because I think is not the best approach for this, I would like to serialize it using the JSON.NET library. So I wrote this:
public class StatusJSON
{
public byte ID { get; set; }
public string Name { get; set; }
public StatusJSON() { }
public StatusJSON(byte id, string name)
{
ID = id;
Name = name;
}
}
public class JSONUtils
{
/// <summary>
/// Get all the posible statuses of selected <paramref name="type"/> in JSON
/// </summary>
/// <param name="type">Type of the status</param>
/// <param name="addDefault">Check if add a default / NULL status</param>
/// <returns>A string JSON with the statuses</returns>
public static string GetStatuses(Type type, bool addDefault = false)
{
var statusesEnum = Enum.GetValues(type);
List<StatusJSON> statuses = new List<StatusJSON>();
if (addDefault)
statuses.Add(new StatusJSON(0, "-"));
foreach (var statusEnum in statusesEnum)
statuses.Add(new StatusJSON((byte)statusEnum, Enum.GetName(type, statusEnum)));
return JsonConvert.SerializeObject(statuses);
}
}
You can use it as: string statuses = JSONUtils.GetStatuses(typeof(StatusEnum), addDefault);. The problem is than this return a string like:
[{"ID":0,"Name":"-"},{"ID":1,"Name":"Active"},{"ID":2,"Name":"Deactive"},{"ID":3,"Name":"Pending"}]
There's any method in the library to get a string like the one I need? Thanks
What I did finally is re-use my old code. So now I have this:
public class Statutes
{
public byte ID { get; set; }
public string Name { get; set; }
public Statutes() { }
public Statutes(byte id, string name)
{
ID = id;
Name = name;
}
/// <summary>
/// Get all the posible statuses of selected <paramref name="type"/>
/// </summary>
/// <param name="type">Type of the status</param>
/// <param name="addDefault">Check if add a default / NULL status</param>
/// <returns>A list with the statuses</returns>
public static List<Statutes> SelectAll(Type type, bool addDefault = false)
{
var statusesEnum = Enum.GetValues(type);
List<Statutes> statuses = new List<Statutes>();
if (addDefault)
statuses.Add(new Statutes(0, "-"));
foreach (var statusEnum in statusesEnum)
statuses.Add(new Statutes((byte)statusEnum, Enum.GetName(type, statusEnum)));
return statuses;
}
}
public class JSONUtils
{
/// <summary>
/// Get all the posible statuses of selected <paramref name="type"/> in JSON
/// </summary>
/// <param name="type">Type of the status</param>
/// <param name="addDefault">Check if add a default / NULL status</param>
/// <returns>A string JSON for jQGrid with the statuses</returns>
public static string GetStatusesJQGrid(Type type, bool addDefault = false)
{
var statuses = Statutes.SelectAll(type, addDefault);
string result = "{value: {";
foreach (Statutes status in statuses)
result += String.Format("{0}: '{1}', ", status.ID, status.Name);
return result.Substring(0, result.Length - 2) + "}}";
}
}
You can use it as: string statuses = JSONUtils.GetStatusesJQGrid(typeof(StatusEnum), true);
Until I'll find a better approach using the JSON.NET I think this is a good piece of code to re-use for the people using jQGrid. This is valid for the select option:
colModel: {name: 'status_id', label: 'Status', edittype: 'select', sortable: true, search: true, stype:'select', editoptions: " + statuses + #", searchoptions: {sopt: ['eq', 'ne']}}
Related
string carPrice = driver.FindElement(By.XPath("//body/main[1]/article[1]/section[1]/section[1]/section[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/h3[1]")).Text;
string basePrice = "USD1000";
carPrice is dynamic value which contains currency and comma such USD42,000
Here are several language extension methods to assist, ignore the array extensions, I had several of these hanging around already.
Full source
public static class Extensions
{
/// <summary>
/// Convert string to decimal
/// </summary>
/// <param name="sender">assumed value has one or more digest</param>
/// <returns>decimal value or will throw an exception if can not convert e.g. an empty string</returns>
public static decimal ToDecimal(this string sender) =>
decimal.Parse(Regex.Replace(sender, #"[^\d.]", ""));
/// <summary>
/// Any value in array greater than checkValue
/// </summary>
/// <param name="sender">valid decimal array</param>
/// <param name="checkValue">check if an element in sender is greater than this value</param>
/// <returns>true if condition is met, false if not met</returns>
public static bool GreaterThan(this decimal[] sender, decimal checkValue) =>
sender.Any(value => value > checkValue);
/// <summary>
/// Is sender greater than checkValue
/// </summary>
/// <param name="sender">valid decimal value</param>
/// <param name="checkValue">is sender greater than this value</param>
/// <returns>true if condition is met, false if not met</returns>
public static bool GreaterThan(this decimal sender, decimal checkValue) =>
sender > checkValue;
public static decimal[] ToDecimalArray(this string[] sender)
{
var decimalArray = Array
.ConvertAll(sender,
(input) => new
{
IsDecimal = decimal.TryParse(Regex.Replace(input, #"[^\d.]", ""), out var doubleValue),
Value = doubleValue
})
.Where(result => result.IsDecimal)
.Select(result => result.Value)
.ToArray();
return decimalArray;
}
}
Unit test
using ConvertingUnitTest.Base;
using ConvertingUnitTest.Classes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ConvertingUnitTest
{
[TestClass]
public partial class MainTest : TestBase
{
[TestMethod]
[TestTraits(Trait.SingleConvert)]
public void TestToDecimal()
{
string mockedBasePrice = "USD1000";
decimal basePrice = mockedBasePrice.ToDecimal();
decimal expected = 1000;
Assert.AreEqual(basePrice, expected);
}
[TestMethod]
[TestTraits(Trait.SingleConvert)]
public void TestStringValueIsNotGreaterThan()
{
string mockedBasePrice = "USD1000";
decimal basePrice = mockedBasePrice.ToDecimal();
string value = "USD999";
Assert.IsFalse(value.ToDecimal() > basePrice);
}
[TestMethod]
[TestTraits(Trait.SingleConvert)]
public void TestStringValueIsGreaterThan()
{
string mockedBasePrice = "USD1000";
decimal basePrice = mockedBasePrice.ToDecimal();
string value = "USD1001";
Assert.IsTrue(value.ToDecimal() > basePrice);
}
[TestMethod]
[TestTraits(Trait.SingleConvert)]
public void TestStringWithWhiteSpace()
{
string mockedBasePrice = "USD1000";
decimal basePrice = mockedBasePrice.ToDecimal();
string value = "USD10 01";
Assert.IsTrue(value.ToDecimal() > basePrice);
}
[TestMethod]
[TestTraits(Trait.ArrayConvert)]
public void TestStringArrayNotGreaterThan()
{
string mockedBasePrice = "USD1000";
decimal basePrice = mockedBasePrice.ToDecimal();
string[] values = { "USD999", "", "USD20" };
var onlyDecimalArray = values.ToDecimalArray();
Assert.IsFalse(onlyDecimalArray.GreaterThan(basePrice));
}
[TestMethod]
[TestTraits(Trait.ArrayConvert)]
public void TestStringArrayIsGreaterThan()
{
string mockedBasePrice = "USD1000";
decimal basePrice = mockedBasePrice.ToDecimal();
string[] values = { "USD999", "USD2020", "USD20" };
var onlyDecimalArray = values.ToDecimalArray();
Assert.IsTrue(onlyDecimalArray.GreaterThan(basePrice));
Assert.IsTrue(onlyDecimalArray[1].GreaterThan(basePrice));
}
}
}
If your pricing format is fixed you can use this //USD42,000
char[] delimiterChars = {',','U','S','D'};
string[] values =carPrice.Split(delimiterChars);
string join = string.Join("", values);
int price = int.parse(join);
int price = int.parse(join);
The above should be:
int price = int.Parse(join);
Just a simple capital P for Parse.
I receive this error when send post form-data to another api, with HttpClient :
I have this parameters on another api controller:
public class IntegracaoArquivosDigitaisDto
{
/// <summary>
/// IdInteral do arquivo digital
/// </summary>
/// <example>1</example>
public int IdInternalArquivoDigital { get; set; }
/// <summary>
/// Chave do Módulo
/// </summary>
/// <example>
/// RecursosHumanos
/// </example>
public string ChaveModulo { get; set; }
/// <summary>
/// Versão do Módulo
/// </summary>
/// <example>
/// 1.6
/// </example>
public int? Versao { get; set; }
/// <summary>
/// Versão dos Dados enviados para processamento
/// </summary>
/// <example>
/// c8283005-68f5-41bf-aea6-29496068656c
/// </example>
public Guid IdProcessamento { get; set; }
/// <summary>
/// Colunas chaves
/// </summary>
public List<PayloadDTO> ColunasChaves { get; set; }
/// <summary>
/// Tipo de operação - I,A,E
/// </summary>
/// <example>1</example>
public string TipoOperacao { get; set; }
/// <summary>
/// Id arquivo Digital
/// </summary>
/// <example>1</example>
public string IdArquivoDigital { get; set; }
/// <summary>
/// Nome Arquivo
/// </summary>
/// <example>teste.pdf</example>
public string NomeArquivoDigital { get; set; }
/// <summary>
/// Arquivo
/// </summary>
public IFormFile Arquivo { get; set; }
}
public class PayloadDTO
{
/// <summary>
/// Nome do campo
/// </summary>
/// <example>
/// Cidade
/// </example>
public string Campo { get; set; }
/// <summary>
/// Valor representativo do campo
/// </summary>
/// <example>José da Silva</example>
public string Valor { get; set; }
/// <summary>
/// Tipo do dado
/// </summary>
/// <example>int</example>
public string Tipo { get; set; }
}
And I this is controller :
[HttpPost]
public async Task<IActionResult> Teste([FromForm] IntegracaoArquivosDigitaisDto dto)
{
var json = JsonSerializer.Serialize(dto, new JsonSerializerOptions
{
WriteIndented = true,
});
return Ok(json);
}
I try using StringContent, Json , FormUrlEncodedContent And MultipartFormDataContent, but not working.
What is the correct way to make a post form-data using httpClient, with these parameters?
This is my implementation tests :
[HttpGet]
public async Task<IActionResult> Get()
{
string url = "http://localhost:5001/teste";
using (MultipartFormDataContent formDataContent = new MultipartFormDataContent())
{
var obj = new
{
IdInternalArquivoDigital = 3,
ChaveModulo = "RecursosHumanos",
Versao = 3,
IdProcessamento = Guid.NewGuid(),
ColunasChaves = new List<PayloadDTO>{
new PayloadDTO{
Campo="CdCategoria",
Tipo= "int",
Valor= "32",
},
new PayloadDTO{
Campo="Id",
Tipo= "int",
Valor= "3",
},
new PayloadDTO{
Campo="Nome",
Tipo= "strig",
Valor= "Samuel",
},
},
TipoOperacao = "I",
IdArquivoDigital = Guid.NewGuid().ToString(),
NomeArquivoDigital = "a7x.jg",
};
var json = JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true, });
//json content
// var jsonContent = new StringContent(json);
// jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json")
// {
// CharSet = "utf-8"
// };
// Dictionary<string, string> dic = new Dictionary<string, string>();
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.Versao), obj.Versao.ToString());
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.IdArquivoDigital), obj.IdArquivoDigital.ToString());
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.IdInternalArquivoDigital), obj.IdInternalArquivoDigital.ToString());
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.IdProcessamento), obj.IdProcessamento.ToString());
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.ChaveModulo), obj.ChaveModulo);
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.NomeArquivoDigital), obj.NomeArquivoDigital);
// dic.Add(nameof(IntegracaoArquivosDigitaisDto.TipoOperacao), obj.TipoOperacao);
// formDataContent.Add(new FormUrlEncodedContent(dic), "dto");
// formDataContent.Add(new StringContent(obj.Versao.ToString()), nameof(IntegracaoArquivosDigitaisDto.Versao));
// formDataContent.Add(new StringContent(obj.IdArquivoDigital.ToString()), nameof(IntegracaoArquivosDigitaisDto.IdArquivoDigital));
// formDataContent.Add(new StringContent(obj.IdInternalArquivoDigital.ToString()), nameof(IntegracaoArquivosDigitaisDto.IdInternalArquivoDigital));
// formDataContent.Add(new StringContent(obj.IdProcessamento.ToString()), nameof(IntegracaoArquivosDigitaisDto.IdProcessamento));
// formDataContent.Add(new StringContent(obj.NomeArquivoDigital), nameof(IntegracaoArquivosDigitaisDto.NomeArquivoDigital));
// formDataContent.Add(new StringContent(obj.TipoOperacao), nameof(IntegracaoArquivosDigitaisDto.TipoOperacao));
// formDataContent.Add(new StringContent(obj.ColunasChaves.ToString()), nameof(IntegracaoArquivosDigitaisDto.ColunasChaves));
// formDataContent.Add(new StringContent(obj.ChaveModulo), nameof(IntegracaoArquivosDigitaisDto.ChaveModulo));
formDataContent.Add(new MultipartFormDataContent(obj.ChaveModulo), nameof(IntegracaoArquivosDigitaisDto.ChaveModulo));
string caminhoArquivo = #"C:\Users\samue\OneDrive\Imagens\a7x.jpg";
using (var fileStream = new FileStream(caminhoArquivo, FileMode.Open))
{
try
{
using (MemoryStream ms = new MemoryStream())
{
await fileStream.CopyToAsync(ms);
var bytes = ms.ToArray();
var fileContent = new ByteArrayContent(bytes);
//file content
// fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
// {
// FileName = "a7x.jpg"
// .Replace("(", string.Empty)
// .Replace(")", string.Empty)
// .Replace(" ", string.Empty),
// Name = "arquivo",
// };
formDataContent.Add(fileContent, nameof(IntegracaoArquivosDigitaisDto.Arquivo), "a7x.jpg");
_client.Timeout = TimeSpan.FromMinutes(2);
var res = await _client.PostAsync(url, formDataContent);
res.EnsureSuccessStatusCode();
}
}
catch (System.Exception ex) when (ex is HttpRequestException || ex is OperationCanceledException)
{
throw;
}
}
}
return Ok();
}
I’m trying to implement a website where new charts can be added by just dropping a DLL into folder. At the time of writing it is not clear what charts are going to be needed and this provides a simple way of deploying new charts without having to redeploy the website in its entirety. I’m using Google Charts to provide the Google functionality and each chart will be displayed
Each type of chart inherits from the following Interface
public enum ChartType
{
BAR,
COLUMN,
PIE,
TABLE
}
public class DataColumn
{
public String ColumnType { get; set; }
public String ColumnValue { get; set; }
}
public interface IChart
{
/// <summary>
/// Dictionary of Columns, Each column is defined as a type and title
/// </summary>
List<DataColumn> Columns { get; set; }
/// <summary>
/// ChartType, What type of Chart; possible values BAR, COLUMN, PIE, TABLE
/// </summary>
ChartType ChartType { get; }
/// <summary>
/// Data - data for the chart
/// </summary>
String Data { get; }
/// <summary>
/// Name of the chart, must be unique used to identify each chart stub
/// </summary>
String Name { get; }
/// <summary>
/// Title - the title that will be displayed above the chart
/// </summary>
String Title { get; }
/// <summary>
/// What position will the legend of there is one.
/// </summary>
String LegendPosition { get; }
}
The following uses the above interface
public class ReferralDownloadCount : IChart, IDisposable
{
List<ChartDemo.DataColumn> columns = null;
public ReferralDownloadCount()
{
columns = new List<ChartDemo.DataColumn>()
{
new ChartDemo.DataColumn() { ColumnType = "String" , ColumnValue = "Date" },
new ChartDemo.DataColumn() { ColumnType = "Number" , ColumnValue = "Referral Count"}
};
}
/// <summary>
/// Returns the chart data
/// </summary>
public String Data
{
get
{
String sql = String.Empty;
String jsonResult = String.Empty;
DataSet ds = null;
DataTable dt = null;
List<ReferralCountData> results = null;
JsonSerializer serializer = null;
try
{
sql = "Select * From[Portal].[ReferralCount] Where DATEDIFF(d, [Download Date], Convert(Date, GETDATE())) < 8 Order By[Download Date] Asc";
ds = DataToolbox.Execute(new SqlConnection(Properties.Settings.Default.DataConnection), sql, CommandType.Text);
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0]; // we really are only expecting one table
results = new List<ReferralCountData>();
serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
foreach ( DataRow dr in dt.Rows)
{
using (ReferralCountData rcd = new ReferralCountData()
{
Label = ((System.DateTime)dr.ItemArray[0]).ToString("dd/MM/yyyy"),
Value = Convert.ToInt32(dr["Referral Count"])
})
{
results.Add(rcd);
}
}
jsonResult = JsonConvert.SerializeObject(results);
}
}
catch ( System.Exception ex)
{
throw ex;
}
finally
{
}
return jsonResult;
}
}
public List<ChartDemo.DataColumn> Columns {
get
{
return columns;
}
set
{
columns = value;
}
}
public ChartType ChartType => ChartType.COLUMN;
public string Name => "REFERRALCOUNT";
public string Title => "Referral Download Count";
public string LegendPosition => "None";
public void Dispose()
{
}
}
The site traverses a directory containing DLLs, and searches for any classes such as the one above to create Charts
Classes which have inherited from IChart are then extracted as each DLL is checked by GetChartPlugins
static List<IChart> GetChartPlugins(List<Assembly> assemblies)
{
List<Type> availableTypes = new List<Type>();
List<Type> alertList = null;
try
{
foreach (Assembly currentAssembly in assemblies)
availableTypes.AddRange(currentAssembly.GetTypes());
alertList = availableTypes.FindAll(delegate (Type t)
{
List<Type> interfaceTypes = new List<Type>(t.GetInterfaces());
return interfaceTypes.Contains(typeof(IChart));
});
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if (exFileNotFound != null)
{
if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
}
catch ( System.Exception ex)
{
throw ex;
}
finally
{
}
// convert the list of Objects to an instantiated list of ICalculators
return alertList.ConvertAll<IChart>(delegate (Type t) { return Activator.CreateInstance(t) as IChart; });
}
However when this runs the attempt to Call currentAssembly.GetTypes() falls over throwing a ReflectionTypeLoadException
Which eventually becomes;
Method 'get_Columns' in type 'ReferralManagementCharts.ReferralDownloadCount' from assembly 'ReferralManagementCharts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
Can anyone see why since the Columns property of ReferralDownlodCount
public List<ChartDemo.DataColumn> Columns {
get
{
return columns;
}
set
{
columns = value;
}
}
Does have a get.
I have basic CRUD for a class ConfigSystem.
I have also two methods in the basic ConfigSystem Service.
public Type Get<Type>(string key, string userId)
{
//get config system
var configEntry = this.repository.GetSingleAsync(cs => cs.Key == key && cs.UserId == userId).Await();
if (configEntry == null)
{
configEntry = this.repository.GetSingleAsync(cs => cs.Key == key).Await();
}
if (typeof(Type).Name == configEntry.Type)
{
return JsonConvert.DeserializeObject<Type>(configEntry.Value);
//invalid config type exception
}
else
{
throw new InvalidCastException($"Cannot get the type of this particular key! ");
}
}
public async Task Set<T>(string key, string userId, T value)
{
CheckKey(key, userId);
// find key and then update its value
var configEntry = await this.repository.GetSingleAsync(cs => cs.Key == key && cs.UserId == userId);
//configEntry.Key = key;
if (configEntry == null)
{
configEntry = await this.repository.GetSingleAsync(cs => cs.Key == key);
}
configEntry.Type = value.GetType().Name;
configEntry.Value = JsonConvert.SerializeObject(value);
await this.repository.UpdateAsync(configEntry);
}
I have a static file Config:
public static Dictionary<string, object> ConfigurationList = new Dictionary<string, object>()
{
{"Currency", Enums.Currency.Lev },
{"Measure", Enums.Unit.LinearMetre },
{"DailyDraftLimit", 6},
{"DeleteDraftPeriod", 5},
{"NotificationStartTime", new TimeSpan(03, 00, 00) }, //give exact time 3AM.},
//TODO: Exclude weekends
{"NotificationPeriod", 24},//24 hours
{"PaymentNotificationPeriod", 24},
{"PaymentReceivers", null}, //what do I put here? if I dont have the ids/ -> we put null if we are not sure about the info or the info is not static!
{"VisitsNotificationPeriod", 24},
{"VisitsInternalReceivers", null},
{"VisitsExternalReceivers", null},
{"TermsNotificationPeriod", 24},
{"TermsReceivers", null }
}
and this is my ConfigSystem class:
public class ConfigSystem : AuditEntity<long>
{
/// <summary>
/// Gets or sets the key.
/// </summary>
public string Key { get; set; }
/// <summary>
/// Gets or sets the type.
/// </summary>
public string Type { get; set; }
/// <summary>
/// Gets or sets the value.
/// </summary>
public string Value { get; set; }
/// <summary>
/// Gets or sets the user identifier.
/// </summary>
public string UserId { get; set; }
}
This is my method in the Startup.
private void CreateDefaultConfig(ApplicationDbContext context, IServiceScope serviceScope)
{
var defaultConfig = DefaultTenantConfig.ConfigurationList;
if (context.ConfigSystems.Count() == 0)
{
var configSystemService = serviceScope.ServiceProvider.GetRequiredService<IConfigSystemService>();
//foreach (var entry in defaultConfig)
//{
// var entityToSaveInDb = configSystemService.CreateAsync()
//}
//create for each record a config system entry in the database
}
//check if in the context of the database there are no configs
}
I have no idea what to do with the object, but I had to keep it in object because I had different type of entries. I need to create them for each tenant and it is a default config if they are not created in the database.
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.