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.
Related
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
{
});
)
I have this LINQ query:
myDataObject.period = (from pe in myDataObject.myEDMXDataObject.MyTable
join product in myDataObject.products
on pe.ID equals product.ID
where pe.LangID == myDataObject.chosenLang.LangID
select pe).Distinct().ToList();
where myDataObject.products is of type
List<MyDataObject> products;
I get myDataObject.products with a similar LINQ query with join and where clauses, like this
myDataObject.products = (from tbl in MyTableName
join tbl2 in MyTableName2
on tbl1.ID equals tbl2.ID
where /* where conditions here */
select tbl).ToList();
It works properly. But I want to keep my code clean so instead of running all those conditions and the join again, I want to pass the data I have found already into the next LINQ query.
I am getting error like this:
A first chance exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll
with inner exception:
Unable to create a constant value of type 'MyWPFApp.MyTableName'. Only primitive types or enumeration types are supported in this context.
Of course, the error is clear, I am doing something which is not allowed.
How can I send result from one LINQ query as a part of another LINQ query?
You are using a JOIN when what you should be using is a WHERE:
// select the distinct IDs
var productIds = myDataObject.products.Select(x => x.ID).Distinct().ToList();
myDataObject.period = myDataObject.myEDMXDataObject.MyTable
.Where(pe => pe.LangID == myDataObject.chosenLang.LangID
&& productIds.Contains(pe.ID))
.Distinct()
.ToList();
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?
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,
};
I'm quite new to entity framework and I'm trying to use the join clause on two entities as follows.
var alertlist = from elogAlert in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert
where elogAlert.No_ != null
join elogAlertDetail in yangkeeDBEntity. Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert_Details
on elogAlert.No_ == elogAlertDetail.AlertID
where elogalertdetail.employee_id == driverid
select new
{
elogalertdetail.employee_id,
elogalertdetail.alert_id,
elogalertdetail.no_,
elogalertdetail.status,
elogalertdetail.created_by,
elogalertdetail.date_created,
};
Hi from the above code I'm getting two errors saying
'Error 1 The name 'elogAlertDetail' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.' and 'linq joint type inference failed to call 'join' error '
Currently the two tables does not have any data. Ill be happy if anyone can help me with this situation
you cant use == when joining with Linq. You need to use equals.
Note that it is not the method .Equals(..) but the keyword
from elogAlert in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert
join elogAlertDetail in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert_Details
on elogAlert.No_ equals elogAlertDetail.AlertID //this line has equals instead of ==
where elogAlert.No_ != null
where elogalertdetail.employee_id == driverid
select new
{
elogalertdetail.employee_id,
elogalertdetail.alert_id,
elogalertdetail.no_,
elogalertdetail.status,
elogalertdetail.created_by,
elogalertdetail.date_created,
};
Look at the documentaion on Linq join
The error you have relates to the order of arguments around the equals operand on join.
The joined table MUST be the RHS of the equals, and the LHS must be in the row you are joining to.
In this instance yangkeeDBEntity is not in the elogAlert row
CF the example in MSDN
from c in categories
join p in products on c equals p.Category into ps
from p in ps
select new { Category = c, p.ProductName };
c is in the row you are joining from, p.category is on the table you are joining to
in addition you also need to use the word equals not == as mentioned above