error in linq query c# - c#

i get want to join between data to list or list to list in asp.net c# with linq
and it gives to me error
that code i write:
var accounts = (from r in AccAffList
join b in topicalDB.Affs on r.Item2 equals b.AffId
select r).ToList();
var transactions = DB.VtigerBotransactions.ToList();
var details = from a in accounts join c in transactions on a.Item1 equals c.Accountid select c;
and that error i get :
Error 8 The type of one of the expressions in the join clause is
incorrect. Type inference failed in the call to 'Join'.
what is wrong it?

Related

Linq - The type of one of the expression in the join clause is incorrect. Type inference failed in the call to 'join'

I have an existing linq query and i need to make some changes to it, I tried joining a inmemory object to the query but i am seeing the compile error, i am using EF core
The type of one of the expression in the join clause is incorrect.
Type inference failed in the call to 'join
My code looks like below.
This is a simplified version of actual issue, here i am trying to join empEligible to the existing Linq query and it is not allowwing to join
EmployeeMaster empM = new EmployeeMaster();
int Id = 5663;
EmployeeEligibility empEligible = _empDBContext.EmpEligibilities.FromSqlInterpolated($"SELECT * FROM dbo.fnGetEmployeeDetails({EmpId})").FirstOrDefault();
empM = (from a in _empDBContext.Departments.AsNoTracking()
join b in _empDBContext.Crieterias on a.RollId equals b.RollId
join c in _empDBContext.Merits.AsNoTracking() on b.MeritId equals c.MeritId
join d in _empDBContext.Orders.AsNoTracking() on c.HistoryId equals d.HistoryId
join e in _empDBContext.Transactions.AsNoTracking() on a.RollId equals e.RollId
join f in empEligible on empEligible.Id equals Id
select new EmployeeMaster
{
});
)
tried this way also , but is giving an error
An expression of type 'EmployeeEligibility' is not allowed in a
subsequent from clause in a query expression with source type
ƍQueryable<<anonymous type: <anonymous type: >' Type inference failed in the call to 'SelectMany'
EmployeeMaster empM = new EmployeeMaster();
int Id = 5663;
EmployeeEligibility empEligible = _empDBContext.EmpEligibilities
.FromSqlInterpolated($"SELECT * FROM dbo.fnGetEmployeeDetails({EmpId})")
.FirstOrDefault();
empM = (from a in _empDBContext.Departments.AsNoTracking()
join b in _empDBContext.Crieterias on a.RollId equals b.RollId
join c in _empDBContext.Merits.AsNoTracking() on b.MeritId equals c.MeritId
join d in _empDBContext.Orders.AsNoTracking() on c.HistoryId equals d.HistoryId
join e in _empDBContext.Transactions.AsNoTracking() on a.RollId equals e.RollId
from v in empEligible where empEligible.Id == Id
select new EmployeeMaster
{
});
)

How do I join 3 tables using linq syntax

This is my code:
// Put together a list of new communities
var communityList = from x in db.CommunityTeams
join y in db.Communities on x.CommunityId equals y.CommunityId
join z in db.CommunityRoles on x.CommunityRole equals z.Role
where x.UserId == userId
select new
{
CommunityName = y.ComunityName,
CommunityRoleName = z.Role
};
The join z in db.CommunityRoles is giving me this error:
the Join clause in incorrect. How do I get the syntax correct?
Your syntax isn't incorrect. The tables you are joining with columns x.CommunityRole and z.Role are not in the same type.
Error CS1941 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
You have probably getting this error and you have to join columns with same types for example int and int. Check both of them must be same.

C# Linq to SQL: The type of one of the expressions in the join clause is incorrect. Type inference failed

I am writing a join query on linq to SQL. I am getting error on join keyword that "Error 14 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'." Could you please help me? Let me know if anything else needed.
var query = (from fd in dbcDefaulter.Fees_Dues
join sd in dbcDefaulter.Student_Details on fd.Student_ID equals sd.Student_ID
orderby fd.Student_ID
select new { fd.Month }).ToList();
What if you cast the id that is an int to a string before comparing?
var query = (from fd in dbcDefaulter.Fees_Dues
join sd in dbcDefaulter.Student_Details
on fd.Student_ID.ToString() equals sd.Student_ID
orderby fd.Student_ID
select new { fd.Month }).ToList();

Linq Nested Query Issue with decimal type

I'm applying this following query in C#:
var query = from b in db.SalesOrderHeaders
where b.SubTotal > (from c in db.Employees
join v in db.EmployeePayHistories
on c.BusinessEntityID equals v.BusinessEntityID
select v.Rate)
select new
{
b.BusinessEntityID,
b.SubTotal,
};
But an error is returned: linq and face error: Operator '>' cannot be applied to operands of type 'decimal' and 'System.Linq.IQueryable<decimal>'.
Both b.subtotal and v.rate are decimal type and I want to compare these two. Any help is appreciated.
The problem is that the inner query returns IEnumerable<decimal> rather than a single value.
If there is guaranteed to be only one record returned from your inner query, you could simply call Single():
where b.SubTotal > (from c in db.Employees
join v in db.EmployeePayHistories
on c.BusinessEntityID equals v.BusinessEntityID
select v.Rate).Max()
If more than one value can be returned from the inner query, then you'll need to figure out exactly how that comparison should work and apply the appropriate aggregate function.
Just add Max at the end of the inner query:
var query = from b in db.SalesOrderHeaders
where b.SubTotal > (from c in db.Employees
join v in db.EmployeePayHistories
on c.BusinessEntityID equals v.BusinessEntityID
select v.Rate).Max()
select new
{
b.BusinessEntityID,
b.SubTotal,
};

LINQ error with ToList

I have this query:
(from r in gServiceContext.CreateQuery("opportunity")
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"]
join n in gServiceContext.CreateQuery("annotation") on r["opportunityid"] equals ((EntityReference)n["objectid"]).Id into opp
from o in opp.DefaultIfEmpty().ToList()
where ((EntityReference)r["new_channelpartner"]).Id.Equals(lProfileProperty.PropertyValue) && ((OptionSetValue)r["new_leadstatus"]).Equals("100000002")
with the ToList() I get this error:
The method 'GroupJoin' cannot follow the method 'Join' or is not
supported. Try writing the query in terms of supported methods or call
the 'AsEnumerable' or 'ToList' method before calling unsupported
methods.
If I take the ToList off I get the same error. Is there away to fix this or am I doing it totally wrong?
Thanks!
Side Note: I'm using the DefaultIfEmpty because I need it to still pull down records even if the record its joined to is NULL.
You are not using your grouping, so this should work:
(from r in gServiceContext.CreateQuery("opportunity")
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"]
join n in gServiceContext.CreateQuery("annotation") on r["opportunityid"] equals ((EntityReference)n["objectid"]).Id
where ((EntityReference)r["new_channelpartner"]).Id.Equals(lProfileProperty.PropertyValue) && ((OptionSetValue)r["new_leadstatus"]).Equals("100000002")
Change
join c in gServiceContext.CreateQuery("contact")
to
join c in gServiceContext.CreateQuery("contact").DefaultIfEmpty()

Categories