In linq i am trying to do like this
select * from tbl1 join tbl2 on tbl1.column1= tbl2.column1 and tbl1.column2 = tbl2.column2
how can i write the above query in Linq.... i tried like this but giving error
var sasi = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable()
on new {
table1.Field<string >["SAPQuotationNo"],
table1.Field<string >["Invoiceno"]}
equals new {
table2.Field<string>["SAPQuotationNo"],
table2.Field <string>["Invoiceno"]
}
Use anonymous types
give the properties names
select something
use DataRow.Field as method with round brackets
var sasi = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable()
on new
{
SAPQuotationNo = table1.Field<string>("SAPQuotationN"),
Invoiceno = table1.Field<string>("Invoiceno")
} equals new
{
SAPQuotationNo = table2.Field<string>("SAPQuotationNo"),
Invoiceno = table2.Field<string>("Invoiceno")
}
select table1;
You can try something like this:
from A in context.A
join B in context.B on new { id = B.ID,//..., type = A.ID,//...}
This is the hint, you can explore.
Related
I want to make this query in linq to sql .
Please help. I am new to linq and having problem to with the group by clause .
Here is the sql query
select count(USERID), d.DEPTNAME from USERS u
join department d on u.DEPTID = d.DEPTID
group by u.DEPTID, d.DEPTNAME
A more direct translation would be like this:
var query =
from u in db.Users
join d in db.Departments on u.DeptId equals d.DeptId
group d by new { d.DeptId, d.DeptName } into g
select new
{
g.Key.DeptName,
Count = g.Count(),
};
Though I think it would be better off written like this:
// looks like we're counting users in each department
var query =
from d in db.Departments
select new
{
d.DeptName,
Count = db.Users.Count(u => u.DeptId == d.DeptId),
};
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.
i need to write sql query in string and pass it to linq like executenonquery() in ado.net
but i need to return data from 4 tables
Example Query :
SELECT *
FROM dbo.GeneralAd INNER JOIN
dbo.Category ON dbo.GeneralAd.FkCategoryID = dbo.Category.CategoryID INNER JOIN
dbo.District ON dbo.GeneralAd.FkDistrictID = dbo.District.DistrictID LEFT OUTER JOIN
dbo.Users ON dbo.GeneralAd.FkApprovedByID = dbo.Users.UserID AND dbo.GeneralAd.FKAddedByID = dbo.Users.UserID where .........
how to return data from 4 tables with executenonquery method in linq ?
I'm not sure why you why you want to return all columns from all tables. That seems unnecessary. You can get to the District and Category fields from the GeneralAd object.
What I would probably do is define a property on the General Ad object that executes a select to get the associated User.
If you did want to do it in Linq, you could use something like this:
var results = (from ga in DataContext.GeneralAds
select new
{
Col1 = ga.Col1,
Col2 = ga.Category.Col2,
Col3 = ga.District.Col3,
Col4 = (from u in DataContext.Users
where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
select u.Col4)
});
You might have to add an Any() method on Col4:
var results = (from ga in DataContext.GeneralAds
select new
{
Col1 = ga.Col1,
Col2 = ga.Category.Col2,
Col3 = ga.District.Col3,
Col4 = ((from u in DataContext.Users
where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
select u.Col4).Any() ?
(from u in DataContext.Users
where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
select u.Col4).First() : -1)
});
I have a query that has the following
var myvar = from table in MyDataModel
where.....
select new MyModel
{
modelvar1 = ...,
modelvar2 = (from..... into anothervar)
}
What I want to do is have modelvar2 be a join between the result I currently get from anothervar with another table in MyDataModel.
Thanks
The parenthesis looks more like a subquery than a join. This is how you do a join.
Example tables from the AdventureWorks database.
using (DataClasses1DataContext context = new DataClasses1DataContext())
{
// If you have foreign keys correctly in your database you can
// join implicitly with the "dot" notation.
var myvar = from prod in context.Products
where prod.ListPrice < 10
select new
{
Name = prod.Name,
Category = prod.ProductSubcategory.ProductCategory.Name,
};
// If you don't have foreign keys you need to express the join
// explicitly like this
var myvar2 = from prod in context.Products
join prodSubCategory in context.ProductSubcategories
on prod.ProductSubcategoryID equals prodSubCategory.ProductSubcategoryID
join prodCategory in context.ProductCategories
on prodSubCategory.ProductCategoryID equals prodCategory.ProductCategoryID
where prod.ListPrice < 10
select new
{
Name = prod.Name,
Category = prodCategory.Name,
};
// If you REALLY want to do a subquery, this is how to do that
var myvar3 = from prod in context.Products
where prod.ListPrice < 10
select new
{
Name = prod.Name,
Category = (from prodSubCategory in context.ProductSubcategories
join prodCategory in context.ProductCategories
on prodSubCategory.ProductCategoryID equals prodCategory.ProductCategoryID
select prodCategory.Name).First(),
};
// If you want to get a list from the subquery you can do like this
var myvar4 = from prodCategory in context.ProductCategories
select new
{
Name = prodCategory.Name,
Subcategoreis = (from prodSubCategory in context.ProductSubcategories
where prodSubCategory.ProductCategoryID == prodCategory.ProductCategoryID
select new { prodSubCategory.ProductSubcategoryID, prodSubCategory.Name }).ToList(),
};
}
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]
FROM [Orders] AS [t0]
INNER JOIN ([Order Details] AS [t1]
INNER JOIN [Products] AS [t2] ON [t1].[ProductID] = [t2].[ProductID]) ON [t0].[OrderID] = [t1].[OrderID]
can be write as
from o in Orders
join od in (
from od in OrderDetails join p in Products on od.ProductID equals p.ProductID select od)
on o.OrderID equals od.OrderID
select o