C# Linq - Issue with joining3 tables - Error CS1941 - c#

I'm trying to fetch the records based on this query
documentList = await (from doc in _dbContext.Documents
join doce in _dbContext.StudentEnrollments on doc.ID equals doce.ID
join enr in _dbContext.Enrollments on doce.EnrollmentID equals enr.Id
where enr.name != null
&& (enr.faiedlattemps = 1 || enr.ReferenceNumber != null)
select doc);
I am getting the
"Correlates the elements of two sequences based on matching key error. The default equality comparer is used to compare keys"
I tried with this as well
var result = from x in _dbContext.Documents
join y in _dbContext.StudentEnrollments
on new { X1 = x.ID } equals new { X1 = y.ID}
join z in _dbContext.Enrollments on new { Z1 = y.EnrollmentID } equals new { Z1 = z.Id}
select new
{
/// Columns
};
Getting the same error

Related

Object result from a left join is null c#

Im trying to do a left join in Linq joining two different list as you can see below:
var fab = (from a in lstt
join b in lstt2 on new { a.PO, a.Line } equals new { b.PO, b.Line } into newab
from ab in newab.DefaultIfEmpty()
select new BomFabConcentrado
{
PO = a.PO,
Line = a.Line,
Fabric1 = ab.Fabric1
}).ToList();
The problem is that the result of ab is null and I got the following error message:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
ab was null.
As extra information lstt contains 900 elements and lstt contains 200.
Does anyone know what is wrong?
Thanks for you help.
Left join use following query
var fab = (from a in lstt
join b in lstt2 on new { a.PO, a.Line } equals new { b.PO, b.Line } into newab
from ab in newab.DefaultIfEmpty()
select new BomFabConcentrado
{
PO = a.PO,
Line = a.Line,
Fabric1 = (ab == null || ab.Fabric1 == null) ? 0 : ab.Fabric1
//or use
Fabric1 = ab?.Fabric1 ?? 0
}).ToList();
Add a null check to your lists. For example like this:
lstt = lstt.Where(a => a != null).ToList();
lstt2 = lstt2.Where(b => b != null).ToList();
var fab = (from a in lstt
join b in lstt2 on new { a.PO, a.Line } equals new { b.PO, b.Line }
into newab
from ab in newab.DefaultIfEmpty()
select new BomFabConcentrado
{
PO = a.PO,
Line = a.Line,
Fabric1 = ab.Fabric1
}).ToList();

How do you Left join 'Is Not Null' using linq?

I am having some issues getting my LINQ statement to work. I am left joining a table, secondTable, where one of the columns can be null but I only need the records where this column is not null. I'm not sure how to get the following into a LINQ expression
LEFT JOIN secondTable b ON a.ID = b.oneTableID AND b.name IS NOT NULL
So far my LINQ is:
var list = await (from one in dbRepository.oneTable
join two in dbRepository.secondTable
on new { name = one.name, phone = one.phone, height = { is not null} } equals new
{ name = two.name, phone = two.phone, height = two.height
into temp
from two in temp.DefaultIfEmpty()
select new.....
Any Ideas?
EDIT 1: I was able to find a solution.
var list = await (from one in dbRepository.oneTable
join two in dbRepository.secondTable
on new { name = one.name, phone = one.phone, height = false } equals new
{ name = two.name, phone = two.phone, height = string.IsNullOrEmpty(two.height)}
into temp
from two in temp.DefaultIfEmpty()
select new.....
You have to use SelectMany possibility to create LEFT JOIN:
var query =
from one in dbRepository.oneTable
from two in dbRepository.secondTable
.Where(two => two.name = one.name && two.phone == one.phone
&& two.height != null)
.DefaultIfEmpty()
select new.....
Try this one:
var list = await (from one in dbRepository.oneTable
join two in dbRepository.secondTable
on new { name = one.name, phone = one.phone}
equals new
{ name = two.name, phone = two.phone}
into temp
from two in temp.DefaultIfEmpty()
where one.height == null || one.height = two.height
select new.....

LINQ query left join multiple columns with null check failing

pt and emod are both in-memory queries returning a tElementRowValuesDBModel type. Both are not executed until after this statement shown.
If I debug and look at the returned results separately, both run successfully and return values.
Could I use Except or not in sub-query, sure!, but I just want to understand if this is a bug or there's a workaround.
Strangely, if i remove any reference to the tempe object, everything runs OK.
Am I missing something?
var RealNewNodes = from p in pt
join e1 in emod on new{X1 = p.tElementRowsDBModelID, X2 = p.tAttributesDBModelID, X3 = p.AttributeValue} equals new {X1 = e1.tElementRowsDBModelID, X2 = e1.tAttributesDBModelID, X3 = e1.AttributeValue } into tempt
from tempe in tempt.DefaultIfEmpty()
where tempe == null //if i remove this line, works OK
select new tElementRowValuesDBModel{
tElementRowsDBModelID = p.tElementRowsDBModelID,
tAttributesDBModelID = p.tAttributesDBModelID,
AttributeValue = p.AttributeValue,
ID = (tempe!=null ? 1 : 0 )//if i remove this line, works OK
};
Perhaps if you didn't do the join, since your join/where means find pt without matches, it would work:
var RealNewNodes = from p in pt
where !emod.Any(e1 => p.tElementRowsDBModelID == e1.tElementRowsDBModelID && p.tAttributesDBModelID == e1.tAttributesDBModelID && p.AttributeValue == e1.AttributeValue)
select new tElementRowValuesDBModel {
tElementRowsDBModelID = p.tElementRowsDBModelID,
tAttributesDBModelID = p.tAttributesDBModelID,
AttributeValue = p.AttributeValue,
ID = 1
};

Convert int to string in linq to entities on join clause

I'm trying to join two tables comparing a column of type int with a column of type varchar, but no mather what I try I get some conversion error...
using (Ginkgo5Entities dataContext = new Ginkgo5Entities())
{
var x = (
from node in dataContext.new_users_teams
join udata in dataContext.new_users_data
on new {
x = SqlFunctions.StringConvert((double)node.ID),
y = "team" }
equals new {
x = udata.value,
y = udata.key }
from parent in dataContext.new_users_teams
where node.lft >= parent.lft && node.lft <= parent.rgt && parent.ID == 6
select new {
team = node.team_name
// udata = udata }
).ToList();
}
This throws
"{The specified method 'System.String StringConvert(System.Nullable`1[System.Double])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.}"
QUESTIONS:
Is it even possible what I'm trying? If so.. can anyone help
I this a bad practice?
Thank you very much!
Try change this query to :
var x = (from node in dataContext.new_users_teams
join udata in dataContext.new_users_data on node.ID equals udata.value.ToString() and udata.key equals "team" }
from parent in dataContext.new_users_teams
where node.lft >= parent.lft && node.lft <= parent.rgt && parent.ID == 6

combine three queries

How can I combine these 3 queries together and return them as datatable:
1:
var TDD = 0;
var queryTDD = (from x in db.GetAll<WMINO>()
join y in db.GetAll<WMCTM>()
on x.PO_ID equals y.Contract_ID
select new
{
TDD = x.Payable,
});
2:
Decimal TotalToDatePayable = 0;
TotalToDatePayable = ((from ori in db.GetAll<WMPORI>()
join ctm in db.GetAll<WMCTM>()
on ori.CTMSysID equals ctm.CTMSysID
select ori.ExB4Taxes).Sum());
3:
var query = from ctm in db.GetAll<WMCTM>()
join vnm in db.GetAll<WMVNM>()
on ctm.VendSysID equals vnm.VendSysID
where ctm.WONOs == workOrder && ctm.TransType == "Purchase Order"
select new
{
ctm.CTMSysID,
ctm.Contract_ID,
ctm.VNM_ID};`
any help is appreciated
Not possible with this because each query has different result and using different columns and condition....
There is no meaning in combining this queries.............

Categories