Convert inner join sql query to linq - c#

I have a sql query which is to be converted to Linq
Select b.title
from TableA as a
inner join TableB as b
on a.Email=b.Email
where a.Title<>b.Title
the query which i have tried is
var query =from s in TableA
join r in TableB
on r.Email equals s.Email
but not able to replicate the where clause which may include many columns
My requirement is i need to compare the 2 tables on a primary key column and then get the other column values which do not match

You need a "select" at the end of the query, and you need to get the inputs in the right order:
var query = from s in TableA
join r in TableB on s.Email equals r.Email
where s.Title != r.Title
select s.Title;
For multiple columns, use an anonymous type:
var query = from s in TableA
join r in TableB
on new { s.Email, s.Foo } equals new { r.Email, r.Foo }
where s.Title != r.Title
select s.Title;

Related

Join list with table in linq

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();

To determine if outer join has a match using linq

I have two tables A and B,which I can outer join with linq. I need a way to find if it is possible to have a boolean along with the query if the outer join has a match. For example, I need a boolean to be true if a record is present in tableA and not in tableB. This can be done in SQL using IF, I was wondering if there was something similar in Linq
var result = from a in tableA
join b in tableB on a.Id equals b.userId into group1
from g1 in group1.DefaultIfEmpty()
select new{id = g1.Id,userId = g1.userId,boolIsPresent =(present in tableA not in tableB)}.ToList();
Currently, you're performing an inner join, not a left outer join, which means only records/objects that exist in both tables are retrieved. thus your bool boolIsPresent would always be true.
edit:
to test if a record is present in tableA and does not have a matching record in tableB just check if g1 != null, i.e:
var result = from a in tableA
join b in tableB on a.Id equals b.userId into group1
from g1 in group1.DefaultIfEmpty()
select new
{
id = g1 != null? g1.Id : enterDefault,
userId = g1 != null? g1.userId : enterDefault,
boolIsPresent = g1 != null
}.ToList();
It will be something like
boolisPresent = b != null
Once you get an Outer join working!

Query returning syntax error in FROM clause

I have 3 tables with various columns:
tableA = id(PK) & name columns
tableB = id(PK), A_ID(foreign key to tableA), name, address, etc columns
tableC = id(PK), A_ID(foreign key to tableA), name columns
I’m trying to use the following query to retrieve values from certain columns within all tables based on tableA name = ‘something’, but always returning syntax errors.
“SELECT tableA.name, tableB.name, tableB.address, tableC.name FROM
tableA, tableB, tableC JOIN tableB ON tableA.id = tableB.A_ID JOIN tableC
ON tableA.id = tableC.A_ID WHERE tableA.name = ‘something’”
You have to remove tables from from statement if you want to use join syntax
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM tableA
INNER JOIN tableB ON tableA.id = tableB.A_ID
INNER JOIN tableC ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'
I suggest you to use aliases, the code could be more readable:
SELECT A.name, B.name, B.address, C.name
FROM tableA A
INNER JOIN tableB B ON A.id = B.A_ID
INNER JOIN tableC C ON A.id = C.A_ID
WHERE A.name = 'something'
Ms Access requires you to specify the type of join: INNER; LEFT; or RIGHT. Access does not recognize just JOIN as a synonym for INNER JOIN.
A query which includes more than one join requires parentheses in the FROM clause.
I also changed your quote characters to plain " and '. The sample query included type-setting quotes. I don't know if they are present in the actual SQL, but I would avoid them.
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM
(tableA
INNER JOIN tableB
ON tableA.id = tableB.A_ID)
INNER JOIN tableC
ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'
If you have the full version of Access available, use the query designer to set up the joins. The designer knows the syntax rules which keep the db engine happy.
otherwise you could do it like:
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM tableA, tableB, tableC
WHERE tableA.id = tableB.A_ID AND tableA.id = tableC.A_ID
AND tableA.name = ‘something
Parado's answer is correct..
Moreover you can simplify the length of the query by using aliases for the table names.. And it is good practice too..
select a.name, b.name, b.address
from tableA a
join tableB b
on a.id = b.A_ID

Linq return all columns from all tables in the join

Lets say I have 2 tables both of them contain dynamic columns and I wish to retrieve a collection of datarow with all the columns from both the tables(later i will bind it to a grid view) after performing left outer join.
Sample Query:
var query = from TableA in ds.Tables[0].AsEnumerable()
join TableB in ds.Tables[1].AsEnumerable() on new { col1 = TableA.Field<Int32>("colA"), col2 = TableA.Field<DateTime>("colB") }
equals new { col1 = TableB.Field<Int32>("colA"), col2 = TableB.Field<DateTime>("colB") }
into GJ
from sub in GJ.DefaultIfEmpty()
select TableA;
Problem:
I want to select tableA and tableB together. The above sample query works and it populates all columns of tableA after left outer join. But i wish to retrieve all the columns from both the tables. Please advice.
Just select both parts into an anonymous type:
var query = from TableA in ds.Tables[0].AsEnumerable()
join TableB in [ ...] on [...] equals [...] into GJ
from sub in GJ.DefaultIfEmpty()
select new { RowA = TableA, RowB = sub };
Each element of the result will have two properties: RowA being a row from TableA, and RowB being a matching row from TableB or null if no rows from TableB matches RowA.

C# LINQ join With Just One Row

I'm trying to make a query that grabs a single row from an SQL database and updates it.
TableA
AId
AValue
TableB
BId
AId
BValue
Ok, so TableA and TableB are linked by AId. I want to select a row in TableB based on AValue using a join. The following query is what I have and only grabs a value from TableB based on AId, I just don't know how to grab a row from TableB using AValue. I know you would need to use a join, but I'm not sure how to accomplish that.
var row = DbObject.TableB.Single(x => x.AId == 1)
row.BValue = 1;
DbObject.SubmitChanges();
Below is a LINQ query to do what you are asking.
var row = (from b in TableB
join a in TableA on a.AId equals b.AId
where a.AValue = yourValue).Single();

Categories