I've created three table's name: emps, emp_project, emp_location
now i want to select one column from each table, but when im executing join query so far getting this this error:
The type of one of the expressions in the join clause is incorrect.
Type inference failed in the call to 'GroupJoin'.
the query which im executing is:
from e in Emp_info
from p in Emp_projects
join l in Emp_locations
on new { e.User_id , p.Project_id } equals new { l.User_id, l.Project_id } into detail
from l in detail
select new
{
e.Middlename,
p.Project_name,
l.Location
};
query.Dump("Join query");
Don't know which of the clauses is causing the error!
My guess, is that the two anonymous types it is trying to compare aren't the same (also ensure that the properties are the same datatype).
Change
on new { e.User_id , p.Project_id } equals new { l.User_id, l.Project_id } into detail
To
on new { UserId = e.User_id, ProjectId = p.Project_id } equals
new { UserId = l.User_id, ProjectId = l.Project_id } into detail
this works for me
void Main()
{
var Emp_info = new List<Info>
{
new Info {Middlename = "Middlename",User_id = "1"}
};
var Emp_projects = new List<Project>
{
new Project{Project_id = "1",Project_name = "Project"}
};
var Emp_locations = new List<LocationInfo>
{
new LocationInfo{Location = "Location",Project_id="1",User_id = "1"}
};
/* your code */
var query = from e in Emp_info
from p in Emp_projects
join l in Emp_locations
on new { e.User_id , p.Project_id } equals new { l.User_id, l.Project_id } into detail
from l in detail
select new
{
e.Middlename,
p.Project_name,
l.Location
};
query.Dump("Join query");
/* your code */
}
class Info
{
public string User_id;
public string Middlename;
}
class Project
{
public string Project_id;
public string Project_name;
}
class LocationInfo
{
public string User_id;
public string Project_id;
public string Location;
}
So far i've come to this solution , by using more than one join at a time.
don't is it the right practice or not.
var query=
from e in Emp_info
join l in Emp_locations on e.User_id equals l.User_id // first join
join p in Emp_projects on l.Location_id equals p.Project_id // second join
select new
{
Name= e.Middlename,
Project = p.Project_name,
Location = l.Location
};
query.Dump();
finally got the answer.. this is working perfectly as i want
var query=
from e in db.emp_info
from p in db.emp_projects
join l in db.emp_locations
on new { username= e.User_id ,project_id=p.project_id } equals new { username=l.User_id,project_id= l.Project_id } into detail
from l in detail
select new
{
e,p,l
};
foreach (var q in query)
{
Console.WriteLine("{0} has done project like {1} in {2}",q.e.Middlename,q.p.project_name,q.l.location);
}
Console.ReadLine();
}
Console.ReadLine();
Related
I am trying to add a List inside a new List using C# and I get the following error:
System.NotSupportedException: 'A type that implements IEnumerable 'System.Collections.Generic.List`1
I tried a few methods to implement it but it didn't go trough.
This is what I have done:
public List<ExtendEmp> GetEmpDatas()
{
var result2 = from emp in db.employees
join dep in db.departments on emp.departmentID equals dep.ID
orderby emp.ID
select new ExtendEmp
{
ID = emp.ID,
fname = emp.fname,
lname = emp.lname,
startWorkYear = emp.startWorkYear,
name = dep.name,
manager = dep.manager,
ExtendShiftz = new List<ExtendShft>()
};
return result2.ToList();
}
The specific list I'm trying to add is ExtendShiftz which is initialised here:
public List<ExtendShft> GetShftDatas()
{
var result3 = from emp in db.employees
join emps in db.employee_shift on emp.ID equals emps.employeeid
join shft in db.shifts on emps.shiftid equals shft.ID
select new ExtendShft
{
ID = shft.ID,
employeeid = emps.employeeid,
fname = emp.fname,
lname = emp.lname,
shiftid = emps.shiftid,
date = shft.date,
startTime = shft.startTime,
endTime = shft.endTime
};
return result3.ToList();
}
and of course i added it inside my model that way:
public ExtendEmp()
{
ExtendShiftz = new List<ExtendShft>();
}
public List<ExtendShft> ExtendShiftz { get; set; }
If you guys have any idea what i am doing wrong there i'd be happy to read.
I have a SQL command wherein I am trying to join multiple columns in a single table which is,
dbo.tbl_Application AS APP on (APP.motor_no = SUBSTRING(R.remarks, CHARINDEX('|', R.remarks) + 1, LEN(R.remarks))) AND (Year(APP.date_created) = YEAR(R.txndate)) AND (APP.motor_no = M.motor_no)
Now I converted it to Linq
CONVERTED TO LINQ
join APP in db.tbl_Application on new
{ motor_no = R.remarks.Substring(5), R.txndate.Value.Year, motor_no2 = M.motor_no }
equals new
{ motor_no = APP.motor_no, APP.date_created.Value.Year, motor_no2 = APP.motor_no }
And then added to my existing linq which is:
EXISTING LINQ
from R in db.vwEtracs_Receipt
join RI in db.vwEtracs_ReceiptItem on new { parentid = R.objid } equals new { parentid = RI.parentid }
join IA in db.vwEtracs_IncomeAccount on new { objid = RI.acctid } equals new { objid = IA.objid }
join M in db.vwQVFS_Motor on new { motor_no = R.remarks.Substring(5) } equals new { motor_no = M.motor_no } into M_join
from M in M_join.DefaultIfEmpty()
join OOI in db.vwQVFS_OOI on new { operator_id = R.payerId } equals new { operator_id = OOI.operator_id } into OOI_join
from OOI in OOI_join.DefaultIfEmpty()
join CR in db.vwQVFS_COR on M.motor_id equals CR.motor_id into CR_join
from CR in CR_join.DefaultIfEmpty()
join F in db.vwQVFS_Franchise on new { or_id = R.objid } equals new { or_id = F.or_id } into F_join
from F in F_join.DefaultIfEmpty()
join T in db.vwQVFS_TODA on M.toda_id equals T.toda_id into T_join
from T in T_join.DefaultIfEmpty()
join RT in db.vwQVFS_RT on new { payer_id = R.payerId } equals new { payer_id = RT.payer_id } into RT_join
from RT in RT_join.DefaultIfEmpty()
join B in db.vwQVFS_Brand on M.brand_id equals B.brand_id into B_join
from B in B_join.DefaultIfEmpty()
If I include the line of code from above, I am getting the error Invalid length parameter passed to the LEFT or SUBSTRING function. but if I remove it, my Linq works perfectly.
What is making causing this error? Thanks in advance.
You getting the error because there is at least one record shorter than 5 characters in your database db.vwEtracs_Receipt.remarks column. So in your SQL, you don't pass length actually but in your linq you use Substring(int length). So the substring length can't be longer than the actual string length
I'm trying to return a list of results, however. Whenever there are no results, I receive the error message which I have posted above. However, it's strange because whenever I add the variable q instead of the return, it just returns no results instead and is fine with this. I would prefer to do it the way which I am currently doing it right now, does anybody know what is wrong with the query? Whenever I run it in LINQPad it works completely fine.
public IQueryable<ClaimNumberReport> GetClaimsByClaimNumber(int ClientID, int ClaimID) {
/*var q = */ return (from d in camOnlineDb.Details
join a in camOnlineDb.Areas
on new { a = d.ClientID, b = d.AreaID ?? 0 }
equals new { a = a.ClientID, b = a.AreaID }
where d.ClientID == ClientID
join r in camOnlineDb.Reasons
on new { a = d.ClientID, b = d.ReasonID ?? 0 }
equals new { a = r.ClientID, b = r.ReasonID }
join sd in camOnlineDb.SuppDepts
on new { a = d.ClientID, b = d.CategoryID ?? 0 }
equals new { a = sd.ClientID, b = sd.CategoryID } into sdd
from sd in sdd.DefaultIfEmpty()
join h in camOnlineDb.Headers
on new { d.ClientID, d.ClaimID}
equals new { h.ClientID, h.ClaimID }
where h.ClaimID == ClaimID
join su in camOnlineDb.Suppliers
on new { h.ClientID, h.SupplierID }
equals new {su.ClientID, su.SupplierID }
join cp in camOnlineDb.ClaimPacks
on new { h.ClientID, h.ClaimID }
equals new { cp.ClientID, cp.ClaimID }
join rev in camOnlineDb.Reviews
on new { h.ClientID, h.ReviewID }
equals new { rev.ClientID, rev.ReviewID }
join revp in camOnlineDb.ReviewPeriods
on new { a = rev.ClientID, b = rev.ReviewPeriodID ?? 0 }
equals new { a = revp.ClientID, b = revp.ReviewPeriodID }
join st in camOnlineDb.Statuses
on new { a = d.ClientID, b = d.StatusID ?? 0 }
equals new { a = st.ClientID, b = st.StatusID }
join stcm in camOnlineDb.StatusCategoryMappings
on new { st.ClientID, st.StatusID }
equals new { stcm.ClientID, stcm.StatusID }
join stc in camOnlineDb.StatusCategories
on new { stcm.StatusCategoryID }
equals new { stc.StatusCategoryID }
where stc.StatusCategoryTypeID == 1
select new ClaimNumberReport {
TypeID = d.ClaimTypeID,
CPAttached = cp.FileName,
ReviewPeriodName = revp.ReviewPeriodName,
ClaimID = d.ClaimID,
Line = d.ClaimLine,
AccountNo = su.AccountNo,
SupplierName = su.SupplierName,
Amount = d.Amount,
Status = st.StatusDesc,
DateSent = d.DateSent,
DayOS = d.DaysOS,
NominalPeriod = d.NominalPeriod,
SLInvoiceNo = d.SLInvoiceNo,
Area = a.AreaDesc,
DebitRef = d.DebitFile,
DebitDate = d.JournalDate,
DeductDate = d.DeductDate,
StatusCategoryID = stc.StatusCategoryID,
StatusCategoryDesc = stc.StatusCategoryDesc,
APLReason = r.ReasonDesc,
ClientID = d.ClientID,
DeptNo = sd.DepartmentID,
DeptName = sd.DepartmentName,
Agreed = d.Agreed
});
/*return q;*/
}
This error is caused by a situation where the query result type has a column/property of non-nullable type but the generated query results in a NULL value.
This could be considered a bug or not. It is hard to see what the L2S team should have done differently here. I think they should have added a better error message. This bug is insidious because it sometimes only strikes in production under unusual data...
Your left join (sd) seem not to match and one of the sd.* properties that you select must be an int. Solve that like this:
DeptNo = (int?)sd.DepartmentID, //Cast to nullable
d.CategoryID ?? 0
What are you doing here? This seems to be a way to make the join compile. It's better to use:
join r in camOnlineDb.Reasons
on new { a = d.ClientID, b = (int?)d.ReasonID }
equals new { a = r.ClientID, b = (int?)r.ReasonID }
This cast makes the anonymous type signatures compatible. The generated SQL should now be faster. If you say x ?? 0 that converts to COALESCE(x, 0) which can prevent index use and such.
While (Goster) field type is integer on Genel_KullaniciMenu context. This query is working. But I changed the type of that field to bit on the database and entity class. then query is not working. It gives error like "LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)'". What may i change on this query? Thanks for your helps.
var query = (from K in
(
(from Genel_Form in _context.Form
join Genel_MenuNesne in _context.MenuNesne on new { GenelFormID = Genel_Form.GenelFormID } equals new { GenelFormID = (Genel_MenuNesne.GenelFormID) } into Genel_MenuNesne_join
from Genel_MenuNesne in Genel_MenuNesne_join.DefaultIfEmpty()
join Genel_KullaniciMenu in _context.KullaniciMenu on new { GenelMenuNesneID = Genel_MenuNesne.GenelMenuNesneID } equals new { GenelMenuNesneID = (Genel_KullaniciMenu.GenelMenuNesneID) } into Genel_KullaniciMenu_join
from Genel_KullaniciMenu in Genel_KullaniciMenu_join.DefaultIfEmpty()
join Genel_FormTuru in _context.FormTuru on Genel_Form.GenelFormTuruID equals Genel_FormTuru.GenelFormTuruID into Genel_FormTuru_join
from Genel_FormTuru in Genel_FormTuru_join.DefaultIfEmpty()
join Genel_MenuGrup in _context.MenuGrup on new { GenelMenuGrupID = (Genel_MenuNesne.GenelMenuGrupID) } equals new { GenelMenuGrupID = Genel_MenuGrup.GenelMenuGrupID } into Genel_MenuGrup_join
from Genel_MenuGrup in Genel_MenuGrup_join.DefaultIfEmpty()
join Genel_KullaniciRol in _context.KullaniciRol on new { GenelKullaniciID = (Genel_KullaniciMenu.GenelKullaniciID) } equals new { GenelKullaniciID = Genel_KullaniciRol.GenelKullaniciID } into Genel_KullaniciRol_join
from Genel_KullaniciRol in Genel_KullaniciRol_join.DefaultIfEmpty()
where
!Genel_FormTuru.FormTuruAdi.Contains("Mobile Ekranlar") &&
(from Genel_RolGrup in _context.RolGrup
where
(from Genel_KullaniciRol0 in _context.KullaniciRol
where
Genel_KullaniciRol0.GenelKullaniciID == genelKullaniciID
select new
{
Genel_KullaniciRol0.GenelRolID
}).Contains(new { GenelRolID = Genel_RolGrup.GenelRolID })
select new
{
Genel_RolGrup.GenelGrupID
}).Contains(new { GenelGrupID = Genel_KullaniciMenu.GenelGrupID })
select new
{
MenuGrupAdi = (Genel_MenuGrup.MenuGrupAdi + "-" + Genel_FormTuru.FormTuruAdi.ToUpper()),
Baslik = (Genel_Form.FormKodu + " " + Genel_MenuNesne.MenuNesneAdi),
Genel_Form.FormNameSpaceAdi,
Genel_Form.FormClassAdi,
Goster = Genel_KullaniciMenu.Goster,
GenelGrupID = Genel_KullaniciMenu.GenelGrupID,
GenelRolID = Genel_KullaniciRol.GenelRolID,
GenelMenuGrupID = Genel_MenuNesne.GenelMenuGrupID,
GenelFormTuruID = Genel_Form.GenelFormTuruID,
GenelMenuNesneID = Genel_MenuNesne.GenelMenuNesneID
}))
group K by new
{
K.GenelRolID,
K.MenuGrupAdi,
K.Baslik,
K.FormNameSpaceAdi,
K.FormClassAdi,
K.GenelMenuGrupID,
K.GenelFormTuruID,
K.GenelMenuNesneID
} into g
orderby
g.Key.GenelMenuGrupID,
g.Key.GenelFormTuruID,
g.Key.GenelMenuNesneID
select new
{
g.Key.MenuGrupAdi,
g.Key.Baslik,
g.Key.FormNameSpaceAdi,
g.Key.FormClassAdi,
Goster = g.Max(p => p.Goster),
}).ToList()
That's my query:
var utenti = from User utente in db.User
join amico in amiciParsed on new { utente.Nome, utente.Cognome } equals new { Nome = amico.first_name, Cognome = amico.last_name }
select utente;
but I got that message:
Local sequence cannot be used in LINQ to SQL implementations of query
operators except the Contains operator.
So how can I resolve the situation? Tried to store in a variable db.User, but nothing change.
Do this:
var utenti = from User utente in db.User.AsEnumerable()
join amico in amiciParsed on
new { utente.Nome, utente.Cognome } equals
new { Nome = amico.first_name, Cognome = amico.last_name }
select utente;
But be careful if you want to extend the query, for example:
var utenti = from utente in (from User utente in db.User
where utente.Name.StartsWith(searchText)
select utente).AsEnumerable()
join amico in amiciParsed on
new { utente.Nome, utente.Cognome } equals
new { Nome = amico.first_name, Cognome = amico.last_name }
select utente;