I am having a issue when writing a below expression
var contactDetails = context.contacts
.Where(s=>s.contact_code == contactCode)
.Join(context.accounts, s => s.)
It will not list down the properties after "."
Any Idea? I have import the using System.Linq too.
var contactDetails = (from con in context.contacts.Where(s=>s.contact_code == contactCode)
join acc in context.accounts
on con.Property equals acc.Property
select con or select acc or select new{con,acc})
select con if you only want contacts
select acc if you only want accounts
select new{con,acc} if you only want both
Note use only one select
Related
I need help to convert the following query to Linq
SELECT c.Code, c.Name
from tblCodes as c
where c.code not in
(select Code from npConsultant where ConsultantName = 'X')
and c.Code < 'AA.0000'
When I try in Linqpad it doesn't seem to understand the into or defaultifempty. Maybe these are inproper methods for what I need to do
Simple answer is use the "let" keyword and generate a sub-query that supports your conditional set for the main entity.
var Objlist= from u in tblCodes
let ces = from ce in npConsultant
select ce.code
where !ces.Contains(u.code)
select u;
try something like that:
var query =
from c in Customers
where !(from n in npConsultant
where n.ConsultantName='X'
select n.Code)
.Contains(c.Code)
&& c.Code < 'AA.0000'
select c.Code, c.Name;
I just do not get how Code can be less than 'AA.0000' ....
I'm creating a select from multiple tables using a union as I need to return a list of activities that has occurred for a particular client on the database. I need to return each union with an added column so I can tell the difference between the results. If I was to do the query in SQL it would look something like this:
SELECT cn.NoteID, cn.Note, cn.InsertedDate, 'Note Added' Notes
FROM Client c
INNER JOIN ClientNotes cn ON cn.ClientID = c.ID
WHERE c.ClientID = #ClientID
UNION
SELECT rc.ID, rc.CommNote, rc.InsertedDate, 'Communication Added' Notes
FROM ReceivedCommunication rc
LEFT JOIN Job j ON j.ID = rc.JobID
WHERE j.ClientID = #ClientID or rc.ClientID = #ClientID
My Question is how in Entity Framework using IQuerable do I return the hard-coded Notes column?
I have something like this so far:
Dim client as IQueryable(Of myresultclass) =
(From c As Client
Join cn As ClientNotes In ClientCompanyNotes On c.ID Equals cn.ClientID
Where c.ClientID = ClientID
Select cn.NoteID, cn.Note, cn.InsertedDate).Union(
From rc As ReceivedCommunication In ReceivedCommunications
Join j As Job In Jobs On j.ID Equals rc.JobID
Where j.ClientID = ClientID or rc.ClientID = ClientID
Select rc.ID, rc.CommNote, rc.InsertedDate)
Thanks for your help
Ok worked it out, should have been obvious. For anyone with the same issue, I had to update my Select from Select cn.NoteID, cn.Note, cn.InsertedDate to:
Select New myresultclass With {
.ActivityID = cn.NoteID,
.ActivityType = "Note Added"
.InsertedDate = cn.InsertedDate
}
for each one of the unions that I had
Thanks
I have a following SQL query how can I convert to lambda expression
select * from ContractItems
where ID in (SELECT distinct contractItemId from ContractPackageItems
where contractPackageId in (SELECT ID from ContractPackage
where ContractID = 680))
from the above query I need to know if row exist or not. If row exist then return true.
-TIA
---Update---
Here is what I got but it is not working
(from contractItem in _entities.ContractItems
where contractItem.ID == (from contractPackageItems in _entities.ContractPackageItems
where contractPackageItems.ContractPackageID == (from contractPackage in _entities.ContractPackages where contractPackage.ContractID == contractId select contractPackage.ID) select contractPackageItems.ContractItemId).Distinct()).Any();
Will this not do what you want?
var results = (from ci in _entities.contractItems
join cpi in _entities.contractPackageItems on ci.ID equals cpi.contractItemId
join cp in _entities.contractPackage on cpi.contractPackageId equals cp.ID
where cp.ContractID = 680
select ci).Any();
How can I write a linq to entities query that includes a having clause?
For example:
SELECT State.Name, Count(*) FROM State
INNER JOIN StateOwner ON State.StateID = StateOwner.StateID
GROUP BY State.StateID
HAVING Count(*) > 1
Any reason not to just use a where clause on the result?
var query = from state in states
join stateowner in stateowners
on state.stateid equals stateowner.stateid
group state.Name by state.stateid into grouped
where grouped.Count() > 1
select new { Name = grouped.Key, grouped.Count() };
I believe you can use a GroupBy followed by a Where clause and it will translate it as a Having. Not entirely sure though.
If you want to compare a variable that is not in the group by (Ex: age), then it would be:
var duplicated = (
from q1 in db.table1
where (q1.age >= 10 )
group q1 by new { q1.firstName, q1.lastName } into grp
where (grp.Count() > 1 )
select new
{
firstName= grp.Key.firstName,
lastName = grp.Key.lastName,
}
);
This is the SQL I want (ClearinghouseKey is a bigint):
select *
from ConsOutput O
where O.ClearinghouseKey IN (
select distinct P.clearinghouseKey
from Project P
Inner join LandUseInProject L on L.ClearinghouseKey = P.ClearinghouseKey
where P.ProjectLocationKey IN ('L101', 'L102', 'L103')
and L.LandUseKey IN ('U000', 'U001', 'U002', 'U003')
)
The inner query is straight forward and gives correct results in LINQPad:
var innerQuery = (from p in Projects
join l in LandUseInProjects on p.ClearinghouseKey equals l.ClearinghouseKey
where locations.Contains(p.ProjectLocationKey)
&& (landuses.Contains(l.LandUseKey))
select new { p.ClearinghouseKey }).Distinct();
But the outer query gives the error: Type arguments from ...Contains..cannot be inferred from usage:
var returnQuery = from o in OperOutput
where (innerQuery).Contains(o.ClearinghouseKey)
select o;
Is it because ClearinghouseKey is a bigint? Any other ways to write this query?
Thanks,
Jeanne
Don't use an anonymous type:
select new { p.ClearinghouseKey })
Should be
select p.ClearinghouseKey)
Also consider using Any instead of Contains (I don't have reasons for choosing one over the other, yet).
where innerQuery.Any(i => i == o.ClearinghouseKey)