how can i write this query in linq?
select * from bills as b inner join customer as c1
On b.shipperID=c1.CustomerID inner join customer c2
On b.ConsigneeID=c2.CustomerID
---------------------------
I need to have it as below:
var result=from p1 in entities.bills
join p2 in entities.customer on p1.shipperID equals p2.customerID
join p3 in entities.customer on p1.consigneeID equals p3.customerID
select p2;
return resuls.Tolist()
Thanks:)
In your SQL you are selecting all so in linq you need to put all objects in your new anonymous type as below.
var result = from p1 in entities.bills
join p2 in entities.customer on p1.shipperID equals p2.customerID
join p3 in entities.customer on p1.consigneeID equals p3.customerID
select new
{
Bills = p1,
Shippers = p2,
Consignees = p3
};
return resuls.Tolist();
or if you need them flattened you'll had to project them property by property.
You ought to use navigation properties in LINQ, something like
from bills in entities.bills
select new
{
bills.Shipper
bills.Consignee
};
Related
I have creatred a query in linqpad and it works but when i try to use it in my blazor app the app throws an error.
I have three tables, tblOpportunity, tblOppStatus and lkpStatus
and an opportunity can have many status's depending on how mature the enquiry is. So i need to get all the latestest status for each opportunity. I also want to be able to filter the dataset so i get only opportunities with staus of x, y and z
void Main()
{
var output=from o in TblOpportunities
join sub in (
//get the latest status ID for the from the joining table
from smax in TblOppStatuses
group smax by smax.OpportunityID
into g
select new
{
OName = g.Key,
MaxS = (from t2 in g select t2.OppStatusID).Max()
})
on o.OpportunityID equals sub.OName
join st in TblOppStatuses on sub.MaxS equals st.OppStatusID
//get the statu sname form the lookup table
join lst in Lkp_Statuses on st.StatusID equals lst.StatusID
join c in TblClients on o.ClientID equals c.ClientID
select new
{
c.ClientName,
o.OpportunityName,
sub.MaxS,
lst.Status,
lst.StatusID
};
int[] filter = { -1, 62};
output.Where(of=>filter.Contains(of.StatusID))
.Dump();
}
The error that comes in the subquery
MaxS = (from t2 in g select t2.OppStatusID).Max()
if i comment this out the query runs but doesn't give me what i want. Can anyone advise what i am doing wrong As I say it works fine in Linqpad.
I have a join on three tables with more then one condition on the join. What I want to have is
List<MyVM> MyList { get; set; }
MyList = (from a in _context.Tab1
join b in _context.Tab2 on a.T1F1 equals b.T2F2 and b.T2F2 equals SomeValue
join c in _context.Tab3 on a.T1F2 equals c.T3F1
orderby a.T1F3
select new MyVM()
{
P1 = a.T1F5,
P2 = a.T1F6,
P3= b.T2F4
P4 = c.T3F3
}
).ToList();
The statement compiles OK with ony one condition on the first join, but once I add the second one it complains.
In SQL this would be on a.T1F1 = b.T2F2 and b.T2F2 = SomeValue
BTW, all columns in the join clauses as well as SomeValue are of type int.
How can I have multiple conditions?
Move your condition in the where clause :
MyList = (from a in _context.Tab1
join b in _context.Tab2 on a.T1F1 equals b.T2F2
join c in _context.Tab3 on a.T1F2 equals c.T3F1
where b.T2F2 == SomeValue
orderby a.T1F3
select new MyVM()
{
P1 = a.T1F5,
P2 = a.T1F6,
P3= b.T2F4
P4 = c.T3F3
}
).ToList();
If you want to avoid using where clause :
MyList = (from a in _context.Tab1
join b in _context.Tab2 on new { firstKey = a.T1F1, secondKey = SomeValue } equals new { firstKey = b.T2F2, secondKey = b.T2F2 }
join c in _context.Tab3 on a.T1F2 equals c.T3F1
orderby a.T1F3
select new MyVM()
{
P1 = a.T1F5,
P2 = a.T1F6,
P3= b.T2F4
P4 = c.T3F3
}
).ToList();
I have a table [A] that has columns such as CreatedBy(ID), AuthorizedBy(ID), SentTo(ID) and I need to join them to a table [B] containing user names (UserID, FullName). How can I write a join that connects each record of table A to multiple records in table B to fill in the CreatedBy/AuthorizedBy/SentTo names using LINQ?
can give try as below , basically you have to join B with A three times
form a in A
join b in B on b.Id = a.Createdby
join b1 in B on b1.Id = a.Authrizedby
join b2 in B on b2.Id = a.SentTo
select new {
a.Id,
CreatedBy= b.FullName,
AuthorizedBy = b1.FullName,
SentTo= b2.FullName};
or
from a in A
select new {
a.ID
CreatedBy= b.FirstOrDefault(a.CreatedBy== b.Id).FullName,
AuthorizedBy = b.FirstOrDefault(a.AuthorizedBy== b.Id).FullName,
SentTo= b.FirstOrDefault(a.SentTo== b.Id).FullName
}
I want to join the following Tables
1. B_Book[1st Table]
-B_BID (Book ID)(PK)
-B_Name
-B_CategroyID (FK)
2. BI_BookInstance [2nd Table]
-BI_IID(Instance ID)
-BI_BID (FK)
-BI_Price
3. BC_BookCategory [3rd Table]
-BC_CategoryID (PK)
-BC_CategoryName
First Join B_Book and BI_BookInstance then join the result of those both with BookCategory.
(1st join)[B_BID equals BI_BID]
(2nd nested join)[result of 1st join B_CategoryID equals BC_CategoryID]
Edit
SQL would be something like the following:
SELECT * FROM
(SELECT * FROM B_Book b JOIN BI_BookInstance bi on b.B_BID = bi.BI_BID) as t1
JOIN BC_BookCategoryID bc on bc.BC_CategoryID = t1.B_CategoryID
What matches your query in LINQ would be the following (and you'll notice the similarity with SQL). I've also included some examples on how to rename the fields returned, such as Price or CategoryName:
var results = from b in B_Book
join bi in BI_BookInstance
on b.B_BID equals bi.BI_BID
join bc in BC_BookCategory
on b.B_CategoryID equals bc.BC_CategoryID
select new
{
// put in whatever fields you want returned here:
b.B_BID,
b.B_CategoryID,
b.B_Name,
bi.BI_BID,
bi.BI_IID,
Price = bi.BI_Price,
bc.BC_CategoryID,
CategoryName = bc.BC_CategoryName
};
I have supposed inner joins (your FKs is not null), so i would like query like this:
var ctx = new YourEntities();
var query = from b in ctx.B_Book
from bi in ctx.BI_BookInstance
from bc in ctx.BC_BookCategory
where b.B_BID == bi.BI_BID && b.B_CategoryID == bc.BC_CategoryID
select new
{
BInstID = bi.BI_IID,
BName = b.B_Name,
BPrice = bi.BI_Price,
BCategory = bc.BC_CategoryName
};
foreach (var item in query)
{
Console.WriteLine(item.BInstID);
Console.WriteLine(item.BName);
Console.WriteLine(item.BPrice);
Console.WriteLine(item.BCategory);
Console.WriteLine("");
}
You can do this without explicitly using linq's join statement, provided that navigation properties are in place:
from b in ctx.B_Book
from bi in b.BookInstances
select new { b.Property1, bi.Property2, b.BookCategory.Name }
I need to write Join statment after writing query in linq
example :
var Query = (from Tab in Db.Employees
select Tab)
as i have some cases to perform join operation so
i need to do it on this Query Query.Join(Join with another Table like Department); I need the Syntax
if (DeptID != -1){ Query.Join(Join with table Department where FkDeptID = DeptID); }
Consider the usage of join in the LINQ 'query syntax':
from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}
Something like this?
var results = (from q in Query
join m in myList on q.SomeID = m.SomeID
select unknown);
Try using this query:
var Query =
from e in Db.Employees
join d in Db.Departments on e.FkDeptID equals d.DeptID into departments
select new
{
Employee = e,
Department = departments.SingleOrDefault(),
};
This works assuming that when e.FkDeptID == -1 that there is no record in the Departments table and in that case Department would be assigned null.
You should never have more than one department for an employee so I've used SingleOrDefault rather than FirstOrDefault.