I'm trying to translate following T-SQL query into LINQ:
SELECT * FROM table_A JOIN table_B ON table_A.key = table_B.key AND
table_A.Trouble <> table_B.Trouble
Stackoverflow is full with similar questions, but in my case there are two conditions but each of them has different operator ("equals to" and "not equals to"). Is there any way to get the same result using LINQ?
You can't use the join syntax, you have to use a where clause to connect the two
var query = from a in table_A
from b in table_B
where a.key = b.key &&
a.Trouble != b.Trouble
select new { a, b };
You can also write this query. It should be work fast
var query = from a in table_A
from b in table_B.where(x=>x.key==a.key && x.Trouble != a.Trouble)
select new { a, b };
Related
I have a snippet of Stored Procedure:
...
SELECT B.BinID, AverageCost, SUM(Qty) AS Qty
FROM #CurrentReturn R INNER JOIN Bins B ON R.BinCode = B.BinCode AND B.StoreroomID = #StoreroomID
...
#StorerroomID is one of the SP parameters.
Now I am trying to translate it into LINQ to Entities,
var AverageCostList = from r in CurrentReturn
join b in BinQuery on new {r.BinCode, b.StoreroomID} equals new {b.BinCode, storeroomID}
It does not work, as the type on the L.H.S. of equals cannot contains fields in b.
So is there any way to translate such an inner join SQL into LINQ?
i would put the B.StoreroomID = #StoreroomID comparison into ther where clause
from r in CurrentReturn
join b in BinQuery
on r.BinCode equals b.BinCode
where b.StoreroomID == storeroomID
Really odd issue that I cant work out.
I am trying to join two tables in a linq statement to only retrieve records where the record in table 1 has no related rows in table 2.
I have used Joins before but for some reason I cant get VS to recognise the second table in the linq statement.
EG.
var result =
(from pc in _dataSource.Payments
join bc in _dataSource.BouncedCheques
on pc.PaymentID != bc.PaymentID //This is where the error occurs, VS does not recognise "bc"
where pc.CustomerNumber == getAccountNumber
& pc.IsDeleted == false
orderby pc.PaymentDate descending
select new PaymentAllocation
{
PaymentId = pc.PaymentID,
PaymentDate = pc.PaymentDate,
CustomerNumber = pc.CustomerNumber,
ChequeReference = pc.ChequeReference,
PaymentValue = pc.PaymentValue,
AllocatedValue = pc.AllocatedValue,
UnallocatedValue = pc.PaymentValue - pc.AllocatedValue,
ReceivedBy = pc.ReceivedBy,
PaymentType = pc.PaymentType,
PostedDate = pc.PostedDate
});
Basically the problem is that the variable "bc" does not seem to be recognised, however I have several other similar Linq queries that all work well
Any ideas?
Your problem is that the syntax for join uses the keyword equals and not standard boolean operators.
Try replacing your join by a cartesian product of your tables:
from pc in _dataSource.Payments
from bc in _dataSource.BouncedCheques
where
pc.PaymentID != bc.PaymentID
&& pc.CustomerNumber == getAccountNumber
& pc.IsDeleted == false
In the join clause you should use the equals keyword:
try:
on pc.PaymentID equals bc.PaymentID
I am trying to execute the following SQL statement using linq:
SELECT TTT.SomeField
FROM Table1 as T, Table2 as TT, Table3 as TTT
WHERE (T.NumberID = 100 OR T.NumberID = 101)
AND T.QuestionID = 200
AND T.ResponseID = TT.ResponseID
AND TT.AnswerID = TTT.AnswerID
Essentially getting one field from a third table based on primary/foreign key relationships to the other 2 tables. It is expected to have a single result every time.
var query = from t in db.Table1
join tt in db.Table2 on t.ResponseID equals tt.ResponseID
join ttt in db.Table3 on tt.AnswerID equals ttt.AnswerID
where (t.NumberID == 100 || t.NumberID == 101) && t.QuestionID == 200
select ttt.SomeField
If you always expect single result, you can wrap this in ().Single(), or, if there might be no results found, in ().SingleOrDefault().
If I understand you correct. You should read something about Joins I guess.
here
I have a LINQ query that I need to use a ternary operator on so certain joins are used based on certain criteria. So this is my query.
var lData = (from r in gServiceContext.CreateQuery("campaignresponse")
join a in gServiceContext.CreateQuery("activityparty") on ((EntityReference)r["activityid"]).Id equals ((EntityReference)a["activityid"]).Id
//tenary statement here
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)a["partyid"]).Id equals c["contactid"]
where ((EntityReference)r["new_distributorid"]).Id.Equals(lProfileProperty.PropertyValue)
select new
{
});
This is what I want to do.
If r["new_distributorid"] == 1 I need to use:
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)a["partyid"]).Id equals c["contactid"]
if r["new_distributorid"] == 2 then I need to use:
join c in gServiceContext.CreateQuery("account") on ((EntityReference)a["partyid"]).Id equals c["accountid"]
and if r["new_distributorid"] == 3 then I need to use:
join c in gServiceContext.CreateQuery("lead") on ((EntityReference)a["partyid"]).Id equals c["leadid"]
So basically is new_distributor == 1 I need to use a certain join if its a 2 I need another join and if its a 3 I need another join.
Is this possible? If it is, how would I go about setting that up?
Thanks!
All that's changing is a single string value, so just determine that string value before you start defining the query:
string tableName = "";
switch(r["new_distributorid"])
{
case(1):
tableName = "contact";
case(2):
tableName = "account";
case(3):
tableName = "lead";
}
string tableID = tableName + "id";
//...
join c in gServiceContext.CreateQuery(tableName)
on ((EntityReference)a["partyid"]).Id equals c[tableID]
Actually I think I might have fixed it, gonna do some testing, I wll post my solution if it works.
I am migrating an older DB system over to LINQ, I'm having trouble converting a few of the SQL statements though:
SELECT * FROM cities
INNER JOIN deals ON cities.cityId = deals.CityID
INNER JOIN countries ON cities.countryID = countries.CountryId
WHERE deals.endDate >= (someDate)
AND countries.CountryId = (1)
AND deals.soldout = (false)
I've done some research, but I can't get it to work. Here is the LINQ statement I came up with:
var deals = from d in db.deals
join city in db.cities on d.CityID equals city.cityId
join country in db.countries on city.countryID equals country.CountryId
where d.endDate > DateTime.Today && country.CountryId == 1 && d.soldOut == false
select d;
Is there some particular way to use 2 joins in LINQ?
Sorry, I had a formatting error: the statement is meant to select all the deals that have a city who's countryID = 1
You do not need the 2nd join if you have a country code for the city...
var deals = from d in db.deals
join city in db.cities on d.CityID equals city.cityId
where d.endDate > DateTime.Today &&
city.CountryId == 1 && d.soldOut == false
select d;
If you want columns from all tables involved, you can use an anonymous type for that.
select new {d, city, country}