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()
Related
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 have one below SQL query, it is more complex having some GroupBy, Conditions, etc. which i skip here in post to make my question easy:
SELECT
SUBSTRING(TSL.ctg_name, 6, 100) AS 'TREKK', *
FROM Filteredctg_timbersettlementline AS TSL
INNER JOIN Filteredctg_timbersettlement AS TS
ON TSL.ctg_timbersettlementid = TS.ctg_timbersettlementid
LEFT JOIN FilteredNew_property AS P
ON TS.ctg_propertyid = P.new_propertyid
WHERE (TS.ctg_timbersettlementid = #TimberSettlementID) AND
(TSL.ctg_reportgroup = 'TREKK')
I tried below Linq:
var Trekks = (from ts in XrmContext.ctg_timbersettlementSet
join tsl in XrmContext.ctg_timbersettlementlineSet
on ts.Id equals tsl.ctg_timbersettlementid.Id
join p in XrmContext.New_propertySet
on ts.ctg_propertyid.Id equals p.New_propertyId
into temp
from p in temp.DefaultIfEmpty()
where ts.ctg_timbersettlementId == TimberSettlementGuid
where tsl.ctg_reportgroup == "TREKK"
select new
{
tsl.ctg_name,
ts.ctg_BasisAllocatedForestryFund,
}).ToList();
It throwing me an 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.
How can i achive LEFT JOIN?
To achieve a left join, you need to use an intermediary DefaultIfEmpty() :
var query = from c in db.Customers
join o in db.Orders
on c.CustomerID equals o.CustomerID into sr
from x in sr.DefaultIfEmpty()
select new {
CustomerID= c.CustomerID, ContactName=c.ContactName,
OrderID = x.OrderID == null ? -1 : x.OrderID};
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
var result = from R in db.Clients.Where(clientWhere)
join RA in db.ClientAgencies on R.SysID equals RA.SysID
join A in db.Agencies.Where(agencyWhere) on RA.AgencyID equals A.AgencyID
join AC in db.AdCommittees on A.AgencyID equals AC.AgencyID into temp
from x in temp.DefaultIfEmpty().Distinct()
select new {R,RA,x};
If user enters CommitteeID this is what I do, but I feel there has to be a better way.
var query = (from R in result
where R.x.CommitteeID == params.CommitteeID
select R.R).Distinct();
return query;
Is there a better way?
How are you using the data. The joins could be hurting you depending on what you're trying to achieve (which is very difficult for us to view without context of your data structures).
I can't fault the linq other than to say that you appear to have a log of data being joined which you may or may not need.
The other problem I have is that you will execute the query when you call DefaultIfEmpty(). This means to do your filter you may hit the database again to calculate it's result.
Could you provide some info on your DB Schema and what you are trying to get from your query?
If you're not using your intermediate query for anything else, I would flip it (filter by committeeID first):
Client GetCommitteeClient(int committeeID)
{
return (
from AC in db.AdCommittees
where AC.CommitteeID == committeeID
join A in db.Agencies.Where(agencyWhere) on AC.AgencyID equals A.AgencyID
join RA in db.ClientAgencies on A.AgencyID equals RA.AgencyID
join R in db.Clients.Where(clientWhere) on RA.SysID equals R.SysID
select R
).SingleOrDefault();
}