I used PetaPoco.Compiled 6.0.441...
I want to select data from two tables with inner join ...
always give me error msg
System.InvalidCastException: Object must implement IConvertible.
pls. help me ...
the code as following ..
[TableName("S_SEQ_LOG")]
class S_SEQ_LOG
{
public string gSeqLogId { get; set; }
public string iYear { get; set; }
public string iMonth { get; set; }
public string iDay { get; set; }
public string iCurrentVal { get; set; }
public string vTableNm { get; set; }
[Ignore]
public S_SEQ_RULE ssr { get; set; }
}
[TableName("S_SEQ_RULE")]
class S_SEQ_RULE
{
public string vTableNm { get; set; }
public string vRule { get; set; }
public string vPrefix { get; set; }
public string iLength { get; set; }
public string iCurrentVal { get; set; }
}
var db = DbManager.Create("SqlServer");
var posts = db.Fetch<S_SEQ_LOG, S_SEQ_RULE, S_SEQ_LOG>(
(a, b) =>
{ a.ssr = b; return a; }
,
#"SELECT A.*,B.*
FROM S_SEQ_LOG A INNER JOIN S_SEQ_RULE B ON
A.vTableNm=B.vTableNm
");
Related
I'm trying to get some data from to Lists using Linq. If I use join with equals using one field in each list it's ok and returns number of records as expected, but if I try use join on pair of fields it returns 0 records. Why?
List<Result> inner_join =
(from egais in rests_egais
//join best in rests_best on egais.Product.AlcCode equals best.Product.AlcCode
join best in rests_best
on new {key1 = egais.Shop, key2 = egais.Product.AlcCode}
equals new { key1 = best.Shop, key2 = best.Product.AlcCode }
where egais.Quantity != best.Quantity
select new Result
{
Shop = egais.Shop,
AlcCode = egais.Product.AlcCode,
FullName = egais.Product.FullName,
Quantity_Egais = egais.Quantity,
Quantity_Best = best.Quantity,
Difference = best.Quantity - egais.Quantity
}).ToList();
EDITED: These are classes to use:
public class Rest
{
public Rest()
{
Product = new Product();
}
public string Shop { get; set; }
public DateTime Date { get; set; }
public double Quantity { get; set; }
public string InformF1RegId { get; set; }
public string InformF2RegId { get; set; }
public Product Product { get; set; }
}
public class Product
{
public Product()
{
Producer = new Producer();
}
public string FullName { get; set; }
public string AlcCode { get; set; }
public double Capacity { get; set; }
public string UnitType { get; set; }
public double AlcVolume { get; set; }
public int ProductVCode { get; set; }
public Producer Producer { get; set; }
}
public class Producer
{
public string ClientRegId { get; set; }
public string FullName { get; set; }
public string ShortName { get; set; }
public int Country { get; set; }
public string Description { get; set; }
}
public class Result
{
public string Shop { get; set; }
public string AlcCode { get; set; }
public string FullName { get; set; }
public double Quantity_Egais { get; set; }
public double Quantity_Best { get; set; }
public double Difference { get; set; }
}
I have a class with 8 properties declared:
public class classProduct
{
public int iProductID { get; set; }
public string sPName { get; set; }
public string sPDescription { get; set; }
public int iPTypeID { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
And I have a method in which I add 4 properties in my list like and ProductDetailsTbls is my Tbl1 and ProductTypeTbls is Tbl2.
public List<classProduct> GetAllProduct()
{
List<classProduct> lst = new List<classProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new classProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
But it returns all properties that I don't want.
If you only want some properties I think you will have to introduce a new class.
My approach for this would be to define a base class with your four properties and then you can classProduct inherit from it.
your base-class then looks like:
public class baseClassProduct
{
public string sPName { get; set; }
public string sPDescription { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
}
and your classProduct:
public class classProduct : baseClassProduct
{
public int iProductID { get; set; }
public int iPTypeID { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
In your GetAllProduct-Method you can work with the baseClass and you'll get only the information you want to.
public List<baseClassProduct> GetAllProduct()
{
List<baseClassProduct> lst = new List<baseClassProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new baseClassProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
Hi Im trying do that query in dapper, but the list contatos return with the values null for my class Client, i don't know what i doing wrong. my class Clt_cadCliente is one Client has several contact (clt_cadContatos). Help please.
var lookup = new Dictionary<int, Clt_cadCliente>();
cn.Query<Clt_cadCliente, Clt_cadContatos, Clt_cadCliente>(#"
SELECT s.*, a.*
FROM Clt_cadCliente s
INNER JOIN Clt_cadContatos a ON s.IdCLiente = a.IdCliente ", (s, a) =>
{
Clt_cadCliente shop;
if (!lookup.TryGetValue(s.IdCliente, out shop))
{
lookup.Add(s.IdCliente, shop = s);
}
shop.Clt_cadContatos.Add(a);
return shop;
}, splitOn: "IdCliente").AsQueryable();
var resultList = lookup.Values;
Class:
public partial class Clt_cadCliente
{
public int IdCliente { get; set; }
public Nullable<int> IdClientePai { get; set; }
public string Codigo { get; set; }
public string Nome { get; set; }
public Nullable<System.DateTime> DataNasc { get; set; }
public string Sexo { get; set; }
public Nullable<int> IdEstCivil { get; set; }
public string CPF { get; set; }
public string RG { get; set; }
public Nullable<System.DateTime> DataAdm { get; set; }
public bool Pendencias { get; set; }
public string DescPendencia { get; set; }
public string Obs { get; set; }
public string PessoaFJ { get; set; }
public string NomeFantasia { get; set; }
public string CodDep { get; set; }
public string AtivoInativo { get; set; }
public bool Ativado { get; set; }
public Nullable<int> IdSitBloq { get; set; }
public Nullable<int> SitBloq { get; set; }
public string Profissao { get; set; }
public string Empresa { get; set; }
public string NomeEsposa { get; set; }
public Nullable<System.DateTime> NascEsposa { get; set; }
public Nullable<int> IdNaturezaPadrao { get; set; }
public Nullable<int> ViaCarteirinha { get; set; }
public Nullable<int> Casa { get; set; }
public Nullable<int> Renda { get; set; }
public Nullable<int> RendaComplementar { get; set; }
public string Naturalidade { get; set; }
public string Banco { get; set; }
public string Agencia { get; set; }
public string CidadeBanco { get; set; }
public bool VeiculoProprio { get; set; }
public Nullable<System.DateTime> DIB { get; set; }
public string Foto { get; set; }
public string Nbeneficio { get; set; }
public Nullable<bool> LiberarExame { get; set; }
public Nullable<System.DateTime> ValidadeExame { get; set; }
public Nullable<int> EnviaBoleto { get; set; }
public Nullable<int> IdUsuario { get; set; }
public Nullable<long> IdClienteGlobal { get; set; }
public virtual IList<Clt_cadContatos> Clt_cadContatos { get; set; }
}
Class 2:
public partial class Clt_cadContatos
{
public int IdContato { get; set; }
public string Nome { get; set; }
public string Telefone { get; set; }
public string Email { get; set; }
public bool AdicionarLista { get; set; }
public Nullable<int> IdCliente { get; set; }
}
I cannot test it of course, but I have a similar situation and if you don't insert, at least, the fields of the table Clt_cadContatos and in particular the name of the field IdCliente of this table, then the library cannot resolve the splitOn parameter
var lookup = new Dictionary<int, Clt_cadCliente>();
cn.Query<Clt_cadCliente, Clt_cadContatos, Clt_cadCliente>(#"
SELECT s.*, a.IdCliente, a.OtherField1, a.OtherField2, etc ....
FROM Clt_cadCliente s
INNER JOIN Clt_cadContatos a ON s.IdCLiente = a.IdCliente ", (s, a) =>
{
Clt_cadCliente shop;
if (!lookup.TryGetValue(s.IdCliente, out shop))
{
lookup.Add(s.IdCliente, shop = s);
}
shop.Clt_cadContatos.Add(a);
return shop;
}, splitOn: "IdCliente").AsQueryable();
var resultList = lookup.Values;
You are splitting on the wrong parameter. You need to split on IdContato.
The split on field tells Dapper where one entity ends and the next begins. If you select s.* followed by a.* you want to split into a second entity on the first field of the table a (I'm assuming that your sql tables resemble your classes.)
I need to Access available Hotel Object From query2, here I am able to access HotelCode value using y.key, but How Can I Access the availableHotel Object from query2.
My Matrix MOdel
public class JsonMatrixModel
{
public class Result
{
public string responseId { get; set; }
public string searchId { get; set; }
public int totalFound { get; set; }
public List<availableHotels> availableHotels { get; set; }
}
public class availableHotels
{
public string processId { get; set; }
public string hotelCode { get; set; }
public string availabilityStatus { get; set; }
public double totalPrice { get; set; }
public double totalTax { get; set; }
public double totalSalePrice { get; set; }
public string currency { get; set; }
public string boardType { get; set; }
public List<rooms> rooms { get; set; }
}
public class rooms
{
public string roomCategory { get; set; }
public List<paxes> paxes { get; set; }
public double totalRoomRate { get; set; }
public List<ratesPerNight> ratesPerNight { get; set; }
}
public class paxes
{
public string paxType { get; set; }
public int age { get; set; }
}
public class ratesPerNight
{
public string date { get; set; }
public double amount { get; set; }
}
}
My Query
Enumerable<IGrouping<string, JsonMatrixModel.availableHotels>> quer2 =
from ff in ddd
from ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice) group ss by ss.hotelCode;
Accessing the Value
foreach (var y in quer2)
{
string ss = y.Key;
}
After you make the grouping, make a projection to a new anonymous object with a property that will have the value of your key, and another could the the list of grouped values for that key.
var quer2 =
from ff in ddd
from ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice)
group ss by ss.hotelCode
select new
{
GroupKey = ss.Key,
GroupValuesList = ss.ToList()
};
Console.WriteLine(quer2.First().GroupKey);
IGroupping is just an IEnumerable with an additional Key property. is this what you want?
var groups = items.GroupBy(p => p.Property);
foreach (var group in groups)
{
Console.WriteLine(group.Key);
foreach (var item in group)
{
Console.WriteLine("\t{0}", item.AnotherProperty);
}
}
I am trying to create a list of customer names that is fetched from a Json call but I get an error:
cannot implicitly convert type System.Collections.Generic.List<char>
to System.Collections.Generic.List<string>
I am using these 2 classes:
Customers:
namespace eko_app
{
static class Customers
{
public static List<CustomerResponse> GetCustomers(string customerURL)
{
List<CustomerResponse> customers = new List<CustomerResponse>();
try
{
var w = new WebClient();
var jsonData = string.Empty;
// make the select products call
jsonData = w.DownloadString(customerURL);
if (!string.IsNullOrEmpty(jsonData))
{
// deserialize the json to c# .net
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);
if (response != null)
{
customers = response.response;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return customers;
}
public class BusinessAssociate
{
public string business_associate_id { get; set; }
public string created_by { get; set; }
public DateTime created { get; set; }
public DateTime modified { get; set; }
public bool? deleted { get; set; }
public string business_id { get; set; }
public string identity_id { get; set; }
public string associate_type { get; set; }
public string name { get; set; }
}
public class Identity
{
public string identity_id { get; set; }
public string created_by { get; set; }
public DateTime created { get; set; }
public DateTime modified { get; set; }
public bool? deleted { get; set; }
public string name { get; set; }
public object identity_type { get; set; }
}
public class ChartOfAccount
{
public string chart_of_accounts_id { get; set; }
public DateTime created { get; set; }
public DateTime modified { get; set; }
public string created_by { get; set; }
public string deleted { get; set; }
public string account_id { get; set; }
public string account_name { get; set; }
public string business_id { get; set; }
public string account_category { get; set; }
public string accounts_groups_id { get; set; }
public string cash_equivalent { get; set; }
public string acc_category { get; set; }
public decimal? balance { get; set; }
public decimal? credit_balance { get; set; }
public decimal? debit_balance { get; set; }
public decimal? absolute_balance { get; set; }
public string balance_type { get; set; }
public decimal? raw_balance { get; set; }
public string extended_name { get; set; }
public string normal_balance_type { get; set; }
}
public class CustomerResponse
{
public BusinessAssociate BusinessAssociate { get; set; }
public Identity Identity { get; set; }
public ChartOfAccount ChartOfAccount { get; set; }
}
public class Messages
{
public string msgs { get; set; }
public string errs { get; set; }
}
public class RootObject
{
public List<CustomerResponse> response { get; set; }
public Messages messages { get; set; }
}
}
}
HomeForm:
private void GetCustomerNameList()
{
// get customers
customerURL = "https://eko-app.com/BusinessAssociate/list_associates/1/sessionId:" + sessionID + ".json";
var customers = Customers.GetCustomers(customerURL);
List<string> customerNames = new List<string>();
foreach (var c in customers)
{
customerNames = c.BusinessAssociate.name.ToList(); <--------error thrown here
}
}
The error is thrown at customerNames = c.BusinessAssociate.name.ToList(); on the HomeForm.
What am I doing wrong in creating a list of customer names?
I think you wanted to add all Customer.BusinessAssociate names to list:
foreach (var c in customers)
{
customerNames.Add(c.BusinessAssociate.name);
}
What you originally written converted each name string to char list.
You're assigning a list of chars (string) into a list of strings.
Try something like this outside of the foreach loop:
customerNames = customers.Select(x => x.BusinessAssociate.name).ToList();
This also makes the initialization of cutomerNames redundant.
Instead of foreach use:
customerNames = customers.Select(c => c.BusinessAssociate.name).ToList();