This question already has answers here:
linq join and count
(2 answers)
Closed 7 years ago.
I have a situation where I want to count the total transaction of the customer. I can do it in a mysql query but I don't know to do it in linq sql.
select c.name, count(r.custid)
from receipt r left join in cust c on (c.custid=r.custid)
group by r.custid
How can I convert it in linq sql?
I wouldn't start with the SQL... I'd start by thinking of what you're counting, which is basically the number of receipt records in the group which matches the customer. It sounds like you want something like:
var query = from customer in db.Customers
join receipt in db.Receipts
on customer.CustomerId equals receipt.CustomerId
into customerReceipts
select new { customer.Name, Count = customerReceipts.Count() };
Note that unlike many "left join" examples in LINQ, we don't use customerReceipts.DefaultIfEmpty() because we only want the count - which should be 0 if there are no receipts for that customer.
Related
This question already has answers here:
Linq to Entity Join table with multiple OR conditions
(3 answers)
Closed 6 years ago.
So what I'm trying to do is :-
SELECT * FROM TableA
JOIN TableB ON TableA.OriginPhoneNumber=TableB.Id OR TableA.DestinationPhoneNumber=TableB.Id
Rather strange query I know! But I'm trying to replicate this in EntityFramework/Linq - looking at all the samples I can see a pretty easy way to do it when the join is using an AND (using anonymous types) but does the same result exist for a OR join?
Just do a cross join with a where clause
var results = from a in db.TableA
from b in db.TableB
where a.OriginPhonenumber == b.Id
|| a.DestinationPhoneNumber == b.Id
select new { A = a, B = b };
It's doubtful that an or in a join condition would be more efficient than this, but it's likely that either would result in the same execution plan. Performance aside it will give the same results.
I used Union
var firstJoin=from tbl in TableA
join c in TableB
on c.OriginPhoneNumber Equals tbl.Id
var secondJoin=from tbl in TableA
join c in TableB
on c.DestinationPhoneNumber Equals tbl.Id
var result=firstJoin.Union(secondJoin)
This question already has answers here:
LINQ - Full Outer Join
(16 answers)
Closed 8 years ago.
I have 3 entities in EntityModel: Customer, CustomerAddress (this is junction entity with Ids and orderOnControl columns and cant be ommited in model) and Address. I need to make linq query to FULL JOIN Customer with Address. This query in SQL is plain:
select e1.[AddressID], e1.City, e3.CustomerID, e3.LastName from [SalesLT].[Address] as e1
full join [SalesLT].[CustomerAddress] as e2 on e1.[AddressID] = e2.[AddressID]
full join [SalesLT].[Customer] as e3 on e2.CustomerID = e3.CustomerID
but I need to write this with linq, I found answers where there are 2 entities with many-to-may relation, but couldn't find any with junction entity, I would appreciate any tips
Give this a shot. This LINQ statement will join Customers to Addresses through the junction table CustomerAddress.
var query = from ca in context.CustomerAddress
join a in context.Address on ca.AddressId equals a.AddressId
join c in context.Customer on ca.CustomerId equals c.CustomerId
select new { a.AddressId, a.City, c.CustomerId, c.LastName };
This question already has answers here:
Make a SQL Query to Nhibernate
(2 answers)
Closed 8 years ago.
How do I get this SQL query to LINQ?
SELECT Customer.name
FROM Company INNER JOIN Customer ON Company.CompanyId = Customer.CompanyId
where CompanyId = 2
You could try this one:
var result = from c in Companies
join cu in Customers
on c.CompanyId equals cu.CompanyId
where c.CompanyId==2
select cu.Name
or
var result = db.Companies
.Join(Customers.Where(x==>x.CompanyId==2),
x=>x.CompanyId,
y=>y.CompanyId,
y=>y.Name);
where Companies and Customers refer to you companies and customers respectively.
This question already has an answer here:
What is meant by 'The specified LINQ expression contains references to queries that are associated with different contexts'
(1 answer)
Closed 8 years ago.
I am trying to join 3 tables using LINQ from 2 different SQL Servers (entities).
Error: The specified Linq expression contains references to queries that are associated with different contexts
var query = from a in EntityA.TableA
join p in EntityA.TableB
on a.PersonID equals p.PersonID
join m in EntityB.TableC
on Convert.ToInt32(a.SourceID) equals m.ID
where p.someID == "100000527"
select m.ID;
Please help me to resolve this.
Answer:
var query = from a in EntityA.TableA
join p in EntityA.TableB
on a.PersonID equals p.PersonID
where p.someID == "100000527"
select a.ID;
IQueryable<int> ID = null;
foreach (var item in query)
{
int sourceID= Convert.ToInt32(item);
ID = (from m in EntityB.TableC
where m.ID == sourceID
select m.ID).Distinct();
}
return ID;
Is this right approach?
You can only write Linq to SQL Queries on one database at a time.
If you want to join the data together, you will have to write the two queries separately, create anonymous type objects from them, then join them together using plain old Linq on objects.
This question already has answers here:
How to do a subquery in LINQ?
(6 answers)
Closed 9 years ago.
SELECT userid FROM userTable
WHERE userid in (select writeuserid FROM boardTable)
C# LINQ expressions, how to use a query?
I have been using EF4.
userTable, boardTable is connected to the DbContext.
Why not have two different LINQ queries, so that your inner query doesn't execute for each iteration of the outer query.
var query1 = (from t in dbContext.boardTable
select t.writeuserid).ToArray();
var query2 = from r in dbContext.userTable
where query1.Contains(r.userid)
select r.userid;
If your situation is as simple as in the question then you cause join in linq
Assume in here you use Entity FrameWork, so you can use Join to get the result, below is to use the lambda expression:
var result = dbContext.Users.Join(dbContext.Boards,
user => user.UserId,
board => board.WriteUserId,
(u, b) => u.UserId);
why not using join?
var result = (from u in dbcontext.userTable
join u1 in dbcontext.boardTable on u.userid equals u1.writeuserid
select u.userid).FirstOrDefault();
if (result != null)
// do anything else
else
// user not exists