How do I return all the columns in the following anonymous linq join:
var results = (from t in Table1.AsEnumerable() join t2 in Table2.AsEnumerable()
on t.Field<string>("id") equals t2.Field<string>("id")
into allcol from rows in allcol
select rows);
I am getting allow rows from Table2, and no rows from Table1
var results = (from t in Table1.AsEnumerable()
join t2 in Table2.AsEnumerable()
on t.Field<string>("id")
equals t2.Field<string>("id")
into allcol
from rows in allcol
select new {table1=t,table2=rows});
I hope this will help.
Related
I am facing issue to apply Right Outer Join in LINQ. I am trying to build the similar query in LINQ by converting from SQL query.
My correct SQL query
select *
from Table1 t1
inner join Table2 t2 on t1.Id = t2.FID and t1.CID = 20
right outer join Table3 t3 on t1.GID = t3.GID
var result = from t1 in Table1
join t2 in Table2
on t1.ID equals t2.FID into gr1
where t1.CID = 20
join t3 in Table3
on t1.GID equals t3.GID into gr2
from t3 in gr2.DefaultIfEmpty()
select new Details (){
}
return result.ToList();
}
My LINQ query is not working as expected to SQL query.
This is also discussed here
how to make a right join using LINQ to SQL & C#
var RightJoin = from adds in dc.EmpresaAddendas
join cats in CatAddendas
on adds.IDAddenda equals cats.IDAddenda into joined
from cats in joined.DefaultIfEmpty()
select new
{
Id = cats.IDAddenda,
Description = cats.Descripcion
};
Try this:
var result = from t1 in Table1
join t2 in Table2
on new { id = t1.ID; cid = t1.CID } equals new { id = t2.FID: cid = 20} into gr1
join t3 in Table3
on t1.GID equals t3.GID into gr2
from t123 in gr2.DefaultIfEmpty()
select new Details (){
}
return result.ToList();
}
As you're discovered, most LINQ providers don't offer support for a right join. DefaultIfEmpty translates into a LEFT join. As a result, you need to flip the logic of your query around a bit to turn the right join into a left one.
As far as the actual syntax, I've had success adopting more of an ANSI-82 syntax rather than worrying about the into temp2 from temp3 in temp2 syntax. Something along the lines of:
var result =
from t3 in Table3
from t1 in Table1.Where(temp1 => temp1.GID == t3.GID).DefaultIfEmpty()
join t2 in Table2 on t1ID equals t2.FID
where t1.CID == 20
select new Details{};
I have a problem. I get all data in var type variable and then want to apply a join with database table in code first approach. Facing problem, lot of search on internet and apply but failed.
var joinedData =
from menuGroup in _menuGroupMenusRepository.GetAll()
.Where(x => x.GroupId == input.GroupId)
join menus in _menuRepository.GetAll()
on menuGroup.MenuId equals menus.Id
join categSubcateg in _menuCategSubCategRepository.GetAll()
on menus.Id equals categSubcateg.MenuId
join categ in _menuCategoryRepository.GetAll()
on categSubcateg.CategoryId equals categ.Id
select new
{
CategoryId = categSubcateg.CategoryId,
CategoryName = categ.Category,
};
Now I want joinedData variable join with MainMenuSort table.
MainMenuSort table also have groupid and categoryid.
to perform join you just need to do as below
var q=(from jd in joinedData
join mms in dataContext.MainMenuSort
on jd.CategoryId equals mms.CategoryId
select jd).ToList();
if its datatable then
var q=(from jd in joinedData
join mms in dtMainMenuSort.AsEnumerable()
on jd.CategoryId equals mms.Field<int>("CategoryId")
select jd).ToList();
I have been trying to get this one to work, but so far I couldn't.
What happen is I have 2 tables Table1 and Table2, I got the records from Table2 to list
List<Table2Name> listTbl2 = (from o in context.Table2 select o).ToList();
List<Table1Name> listTbl1 = new List<Table1Name>();
In listTbl2, there is a column ID, Table1 also has same col ID...
Now I want to get records from Table1 that has matching column ID with the listTbl2, how do I do that?
NoobieCoder,
try this.
var results = (from t1 in context.Table1
join t2 in listTbl2 on t1.ID equals t2.ID
select t1).Distinct();
I have two Data Tables: T1 and T2
T1 and T2 both have a Column Registration, but T2 doesn't have all the same Rows as T1.
I have to combine the Two Tables such that if the Registration Number is same get columns from T2, if not show Blank, but I need ALL ROWS FROM T1, (IF MATCH OR NOT).
I tried this but I only get matching Rows:
var results = from table1 in T1
join table2 in T2
on (String)table1["Registration"] equals (String)table2["Registration"]
select new
{
Registration = (String)table1["Registration"],
DistanceInKM = (decimal)table1["DistanceInKM"],
TotalDistanceTravelledKM = (Double)table2["TotalDistanceTravelledKM"]
};
You're performing an inner join, which only shows rows that exist on both sides.
Try using a left outer join instead:
var results = (from table1 in T1.AsEnumerable()
join tmp in T2.AsEnumerable() on table1["Registration"] equals tmp["Registration"] into grp
from table2 in grp.DefaultIfEmpty()
select new
{
Registration = (String)table1["Registration"],
DistanceInKM = (decimal)table1["DistanceInKM"],
TotalDistanceTravelledKM = (table2 == null ? (double?)null : (Double)table2["TotalDistanceTravelledKM"])
};
What is an equivalent LINQ query to the following SQL query:
Select Id, Name
From Table1 tbl1
Where Id in ( Select Id From Table2)
You basically described an inner join with selection of rows from one table:
var result = from t1 in Table1
join t2 in Table2
on t1.Id equals t2.Id
select new{t1.Id, t1.Name};
Next SQL statement will be generated using EF and MS SQL:
SELECT t1.Id, t1.Name
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.Id = t2.Id
Note, that if you are selecting non-unique items from Table2, you can potentially have duplicates in the result. Use next query to avoid this problem, cons: it loads all ids from Table2 into memory, pros: more time efficient. Checkout Felipes answer's, which is also quite good, but has it's own cons discussed in the comment section.
var table2Ids = new HashSet<int>(context.Table2.Select(t2 => t2.Id));
var result = context.Table1
.Where(t => table2Ids.Contains(t.Id))
.Select(t => new{t.Id, t.Name});
try using the Contains method:
var query = from c in db.Table1
where db.Table2.Select(x => x.Id).Contains(c.Id)
select new { c.Id, c.Name };
var result = query.ToList();