Invalid column id - c#

I'm trying to get a line from my db
var listItem = ctx.wc_duty_list.FirstOrDefault(x => x.DutyId == param.DutyId);
Here is the table cs file generated by EF
public partial class wc_duty_list
{
public int DutyId { get; set; }
public string AircraftRegistration { get; set; }
public Nullable<int> Priority { get; set; }
public string Description { get; set; }
public string CreatedByStaffCode { get; set; }
public Nullable<System.DateTime> CreatedTime { get; set; }
public string StartedByStaffCode { get; set; }
public Nullable<System.DateTime> StartedTime { get; set; }
public string ClosedByStaffCode { get; set; }
public Nullable<System.DateTime> ClosedTime { get; set; }
public Nullable<int> WorkcardUnscheduledId { get; set; }
public Nullable<int> WorkcardScheduledId { get; set; }
public string AssignedStaffCode { get; set; }
public Nullable<System.DateTime> AssignedTime { get; set; }
public Nullable<int> PackageCheckId { get; set; }
public Nullable<int> Status { get; set; }
public byte[] Concurrency { get; set; }
public Nullable<int> DutyPriviledgeId { get; set; }
public Nullable<int> DutyScopeId { get; set; }
public Nullable<int> AtaId { get; set; }
public Nullable<int> DutyListTagId { get; set; }
public Nullable<int> PackageId { get; set; }
public Nullable<int> WorkcardInhouseId { get; set; }
public string AirportCode { get; set; }
public Nullable<int> Workcard3rdId { get; set; }
public string Note { get; set; }
public Nullable<int> DutyActionId { get; set; }
public Nullable<int> SignatureStatus { get; set; }
public virtual wc_duty_action wc_duty_action { get; set; }
public virtual ev_wc_dutylist_details ev_wc_dutylist_details { get; set; }
public virtual wc_duty_list_priority wc_duty_list_priority { get; set; }
}
When running my code, I get this error:
Would you kindly point me in the right direction ? :)
Cheers!

You are looking for a Where() instead of FirstorDefault();
var listItem = ctx.wc_duty_list.Where(x => x.DutyId == param.DutyId).FirstOrDefault();
FirstOrDefault returns an instance of the particular items in the Enumerable. Where limits the original list to the items that match the supplied criteria.

Related

Creating Types in Elasticsearch with NEST

So I a built a very basic Elastic example using just one Type (Products) I was able to use automap to build this and all worked well, now I want to expand on this and add more types I am running into issues but without any real exceptions being thrown to give me a clue as to what is going wrong.
class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string ProductCode { get; set; }
public string Barcode { get; set; }
public Nullable<int> ProductCategoryId { get; set; }
public int Length { get; set; }
public int Height { get; set; }
public int ProductTypeId { get; set; }
public string Url { get; set; }
public Nullable<int> ProductBrandId { get; set; }
public int Width { get; set; }
public string Html { get; set; }
public string Description { get; set; }
public string MetaTitle { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public decimal CostPrice { get; set; }
public Nullable<int> ProductBuyingPriceId { get; set; }
public int DispatchTimeInDays { get; set; }
public int LeadTimeInDays { get; set; }
public string ManufacturerPartNumber { get; set; }
public string Notes { get; set; }
public int StockAvailable { get; set; }
public decimal WeightKg { get; set; }
public Nullable<int> SellingPriceGroupId { get; set; }
public Nullable<decimal> ReviewRating { get; set; }
public int ReviewRatingCount { get; set; }
public bool NonReturnable { get; set; }
public bool LimitedStock { get; set; }
public Nullable<int> TaxRateId { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
}
class ProductCategory
{
public int ProductCategoryId { get; set; }
public string Name { get; set; }
public Nullable<int> ParentProductCategoryId { get; set; }
public string FullPath { get; set; }
public string Code { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
var des = new CreateIndexDescriptor("myindex")
.Mappings(ms => ms
.Map<Product>(m => m.AutoMap())
.Map<ProductCategory>(m => m.AutoMap())
);
var res = elasticClient.CreateIndex(des);
Output.AppendText(res.ToString());
Output.AppendText(Environment.NewLine);
Output.AppendText("Index Created");
Output.AppendText(Environment.NewLine);
}
As mentioned if I just Include it works fine if I add Product Category nothing happens. TIA
Thanks for the responses As Russ said I was trying to have multiple types which wouldn't work

CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<

I'm using this code but doesn't work
List<UrunListesi> DegiskenListesi = new List<UrunListesi>();
DegiskenUrunListesi = (from UL in DB.UrunListesi
where IDListesi.Contains(UL.KategoriID.ToString()) && UL.Durum == true
orderby UL.id descending
select UL).ToList();
Browser Error
"CS0029: Cannot implicitly convert type
System.Collections.Generic.List<UrunListesi>
[c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll]' to
System.Collections.Generic.List<UrunListesi>
[c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll]'"
I tried this line but this didn't work too
IEnumerable<UrunListesi> DegiskenUrunListesi = null;
DegiskenUrunListesi = (from UL in DB.UrunListesi
where IDListesi.Contains(UL.KategoriID.ToString()) && UL.Durum == true
orderby UL.id descending
select UL).ToList();
dbcontext
public partial class MySiteDBEntities : DbContext
{
public MySiteDBEntities()
: base("name=MySiteDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AnaBanner> AnaBanner { get; set; }
public virtual DbSet<BankaListesi> BankaListesi { get; set; }
public virtual DbSet<KategoriListesi> KategoriListesi { get; set; }
public virtual DbSet<KrediKartiOdemeleri> KrediKartiOdemeleri { get; set; }
public virtual DbSet<TaksitOranTablosu> TaksitOranTablosu { get; set; }
public virtual DbSet<UrunResimListesi> UrunResimListesi { get; set; }
public virtual DbSet<UrunYorumlari> UrunYorumlari { get; set; }
public virtual DbSet<MarkaListesi> MarkaListesi { get; set; }
public virtual DbSet<IlceListesi> IlceListesi { get; set; }
public virtual DbSet<SehirListesi> SehirListesi { get; set; }
public virtual DbSet<UlkeListesi> UlkeListesi { get; set; }
public virtual DbSet<UyeBilgileri> UyeBilgileri { get; set; }
public virtual DbSet<KullaniciListesi> KullaniciListesi { get; set; }
public virtual DbSet<SepetListesi> SepetListesi { get; set; }
public virtual DbSet<BinListesi> BinListesi { get; set; }
public virtual DbSet<RenkListesi> RenkListesi { get; set; }
public virtual DbSet<UrunListesi> UrunListesi { get; set; }
}
UrunListesi Class
public partial class UrunListesi
{
public int id { get; set; }
public Nullable<int> MarkaID { get; set; }
public Nullable<int> KategoriID { get; set; }
public string UrunAdi { get; set; }
public string UrunKodu { get; set; }
public string UrunAciklama { get; set; }
public string AnaResim { get; set; }
public Nullable<decimal> Fiyat { get; set; }
public Nullable<bool> Indirimli { get; set; }
public Nullable<decimal> IndirimliFiyat { get; set; }
public Nullable<int> Kdv { get; set; }
public Nullable<bool> Iskontolu { get; set; }
public Nullable<int> IskontoOrani { get; set; }
public Nullable<int> StokAdet { get; set; }
public Nullable<int> RenkID { get; set; }
public Nullable<bool> EviminoImalati { get; set; }
public Nullable<bool> KargoBedava { get; set; }
public Nullable<decimal> KargoFiyati { get; set; }
public Nullable<int> KargoSuresi { get; set; }
public Nullable<bool> FirsatUrunu { get; set; }
public Nullable<System.DateTime> FirsatBaslangicTarihi { get; set; }
public Nullable<System.DateTime> FirsatBitisTarihi { get; set; }
public Nullable<bool> KampanyaliUrun { get; set; }
public Nullable<System.DateTime> KampanyaBitis { get; set; }
public Nullable<int> UrunHit { get; set; }
public Nullable<System.DateTime> KayitTarih { get; set; }
public Nullable<bool> Durum { get; set; }
public Nullable<System.Guid> UrunKey { get; set; }
}
need help, Thank you.
try:
var list = (from UL in DB.UrunListesi
where IDListesi.Contains(UL.KategoriID.ToString()) && UL.Durum == true
orderby UL.id descending
select UL).ToList();
I solved the problem.
I have UrunListesi Class in dbcontext. i have same name UrunListesi.aspx file. Thats the whole problem.
thanks for all answer.

What would be the linq for this SQL Group Join

I use to work in SQL Server ,Having issue in converting same in Linq .
Its a Group Join in Linq.
SQL Query :
Select
Convert(varchar,CreateOn,101),
count(*),
Note as Total
from TrnExpense
group by Convert(varchar,CreateOn,101),Note
order by Convert(varchar,CreateOn,101) desc
My Output in SQL server follows :
Output in linq is also accepted same .Any help will be appreciated .
Class Generated for TrnExpense follows :
public partial class TrnExpense
{
public int id { get; set; }
public Nullable<int> TripId { get; set; }
public Nullable<int> TruckId { get; set; }
public Nullable<int> TrailerId { get; set; }
public Nullable<int> DriverId { get; set; }
public Nullable<int> ReferType { get; set; }
public Nullable<int> VendorId { get; set; }
public Nullable<int> ExpenseType { get; set; }
public Nullable<int> CountryId { get; set; }
public Nullable<int> StateId { get; set; }
public Nullable<int> CityId { get; set; }
public Nullable<double> Qty { get; set; }
public Nullable<int> qbStatus { get; set; }
public Nullable<int> qbTaxCode { get; set; }
public string QtyUnit { get; set; }
public Nullable<decimal> Rate { get; set; }
public Nullable<decimal> PreTaxAmt { get; set; }
public Nullable<decimal> Outstanding { get; set; }
public string InvoiceNo { get; set; }
public Nullable<double> ServiceFee { get; set; }
public Nullable<decimal> HstTax { get; set; }
public Nullable<double> TaxOne { get; set; }
public Nullable<double> TaxTwo { get; set; }
public string Currency { get; set; }
public string CardNo { get; set; }
public string UnitNo { get; set; }
public Nullable<int> PaymentId { get; set; }
public Nullable<int> SettlementId { get; set; }
public Nullable<int> VendorPayId { get; set; }
public string Note { get; set; }
public Nullable<System.DateTime> ExpenseDate { get; set; }
public Nullable<System.DateTime> DueDate { get; set; }
public string OwnerDeductionYN { get; set; }
public Nullable<int> Status { get; set; }
public Nullable<int> PayStatus { get; set; }
public Nullable<int> DriverDedStatus { get; set; }
public Nullable<int> DriverSettleId { get; set; }
public Nullable<int> UpdateBy { get; set; }
public Nullable<System.DateTime> LastUpdate { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public Nullable<int> CompanyId { get; set; }
public Nullable<decimal> AmountPaid { get; set; }
public Nullable<int> ExpPaymentType { get; set; }
public Nullable<bool> IsExcludeTax { get; set; }
public Nullable<double> Discount { get; set; }
public Nullable<System.DateTime> TransferToQBDate { get; set; }
public string CurrencyCode { get; set; }
public Nullable<int> DocumentUploadId { get; set; }
public string DeductionTypeFlag { get; set; }
public Nullable<decimal> NetAmount { get; set; }
public bool IsActive { get; set; }
public Nullable<System.DateTime> CreateOn { get; set; }
public string CreateBy { get; set; }
public Nullable<System.DateTime> ModifyOn { get; set; }
public string ModifyBy { get; set; }
public Nullable<decimal> Rate1Per { get; set; }
public Nullable<decimal> Rate2Per { get; set; }
public Nullable<bool> TaxIncluded { get; set; }
public Nullable<bool> TaxOnly { get; set; }
public Nullable<bool> IncludeTax1 { get; set; }
public Nullable<bool> IncludeTax2 { get; set; }
public string Source { get; set; }
public Nullable<int> SourceId { get; set; }
public string QbId { get; set; }
public Nullable<System.DateTime> QbLastSyncDate { get; set; }
public virtual CityState CityState { get; set; }
public virtual Country Country { get; set; }
public virtual Driver Driver { get; set; }
public virtual ExpenseType ExpenseType1 { get; set; }
public virtual StateCountry StateCountry { get; set; }
public virtual Vehicle Vehicle { get; set; }
public virtual Vehicle Vehicle1 { get; set; }
public virtual Vendor Vendor { get; set; }
}
var result = (from t in TrnExpense
group t by new { t.CreatedOn.Date, t.Note } into g
orderby g.Key.Date descending
select new{
CreatedOn = g.Key.Date,
Note = g.Key.Note,
Count = g.Count()
}).ToList();
Please try following code ,Written in lambda expression:
var resultSet=TrnExpenses.GroupBy(s=>new
{
s.CreateOn.Value.Date, s.Note
})
.OrderByDescending(s=>s.Key.Date)
.Select(s=>new
{
s.Key.Date,
s.Key.Note,
Count = s.Count()
});
I just tried and found the following and simply want to add on :
To access the date From datetime Column.Value.Date is used .
Following code will clear the same .
(from t in TrnExpenses
group t by new { t.CreateOn.Value.Date, t.Note } into g
orderby g.Key.Date descending
select new{
CreatedOn = g.Key.Date,
Note = g.Key.Note,
Count = g.Count()
}).ToList()

How to use Fluent API to map foreign key

I am trying to use modelBuilder to map a foreign Key in my database. I need to post the Id into JobTESPM_EmployeeId instead of JOBTESPMId. I have been using this a guide but I can't see where the examples are the same as my setup. http://msdn.microsoft.com/en-in/data/jj591620.aspx#IndependentAssociation
public class Job
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public Int64? JobId { get; set; }
public int? JobNumber { get; set; }
public string JobName { get; set; }
public int? JobTypeId { get; set; }
public int? CustomerEmployeePMId { get; set; }
public virtual CustomerEmployee CustomerEmployeePM { get; set; }
public int? CustomerEmployeeAdminId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAdmin { get; set; }
public int? CustomerEmployeeAccountantId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAccountant { get; set; }
public int? CustomerEmployeeSuperintendentId { get; set; }
public virtual CustomerEmployee CustomerEmployeeSuperintendent { get; set; }
public int? JobTESPMId { get; set; }
public virtual Employee JobTESPM { get; set; }
public int? JobTESSuperintendentId { get; set; }
public virtual Employee JobTESSuperintendent { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string AccountName { get; set; }
public string EmployeeFirstName { get; set; }
public string EmployeeLastName { get; set; }
public string EmployeeTitle { get; set; }
public Int64? EmployeeCellPhone { get; set; }
public Int64? EmployeeOfficePhone { get; set; }
public string EmployeeEmail { get; set; }
public int? CompanyEmployeeId { get; set; }
public bool? EmployeeHidden { get; set; }
public bool? EmployeeIsSuper { get; set; }
public bool? EmployeeIsPM { get; set; }
public string EmployeeAltEmail { get; set; }
}
What about adding a couple attributes...
[Column("JobTESPM_EmployeeID")]
public int? JobTESPMId { get; set; }
[ForeignKey("JobTESPMId")]
public virtual Employee JobTESPM { get; set; }

Dapper c# Inner join

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.)

Categories