I have my Table1 that contains the following columns.
My Table2 contains
I am currently getting all the rows from Table1 that are in a date range as follows:
dbRowList = context.Table1.Where(x => x.dateTime > from && x.dateTime < to).ToList();
However, I would like to also get only the rows that, for the same idCycle in Table2 (=id of Table1), have the 'label' field with certain string content.
How to join it or query it using LINQ?
Table1 join Table2 on Table1.id = Table2.idCycle.
Filter for the date range for the records in Table1 and Label in Table2.
Select all columns (or specify the column that is required) from Table1.
var result = (from a in context.Table1
join b in context.Table2 on a.id equals b.idCycle
where (a.dateTime > from && a.dateTime < to)
and b.label = /* Value for Label to filter */
select a
)
.ToList();
Related
I have two data tables as following
TableA TableB
UID QID
QID Value1
Value2
I want to join two data tables and get Value1 and Value 2. I tried the following, but I am not getting any data back.
string str = "AAA";
var result = (from t1 in TableA.AsEnumerable()
//join between two tables
join t2 in TableB.AsEnumerable() on t1.Field<string>("UID") equals t2.Field<string>("QID")
//where conditions
where t2.Field<string>("QID") == str && t1.Field<string>("QID") == "ABCD"
//select clause
select t2.Field<string>("Value1"));
This is my query with out using LINQ and its working.
Select t2.Value1, t2.value2 from TableA t1 JOIN TableB t2 ON t2.QID = t1.UID WHERE t2.QID = 'AAAAA' AND t1.QID = 'XXXX'
Thanks for your help.
Your LINQ code should work, I guess you just need to use the same where the condition values for QID.
where t2.Field<string>("QID") == "AAA" && t1.Field<string>("QID") == "ABCD"
is not equal to your SQL:
WHERE t2.QID = 'AAAAA' AND t1.QID = 'XXXX'
It should work when you set correct values in LINQ
I have a requirement to add multiple where conditions with or operator out of which one where condition will have to check if the db column has any of the item in a list provided. Please consider the query below
var res= from table1 in context.table1
join table2 in context.table2
on table1.id equals table2.id
where table1.name=="res1" || table1.description=="desc"
|| table1.name.any(res=>FreeText.Contains(res))
select table1
this query is leading the compiler to run query multiple times and I am not getting required result. My end goal is to achieve the following sql query
select * from table1 join table2 on table1.id ==table2.id
where table1.name=="res1" || table1.description=="desc" || table1.name like "%item1%" ||table1.name like "%item2% ......"
the like statements should be dynamically added based on the items in the list.
try this...
var res= from table1 in context.table1
join table2 in context.table2
on table1.id equals table2.id
where table1.name=="res1" || table1.description=="desc"
|| FreeText.Any(p => table1.name.Contains(p, StringComparison.InvariantCultureIgnoreCase))
select table1
I need to be able to do this query in linq
select t1.*, t2.column1, t2.column2
from Table1 as t1 inner join Table2 as t2
Where t2.x = true
the main table we want to get all his columns and some other columns from other tables. Can this be done in linq?
from element in t1
join otherElement in t2
on element.match equals otherElement.match
where t2.x == true
select new {
column1 = otherElement.column1,
column2 = otherElement.column2
//add all the elements of t1 here
}
A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence.
https://msdn.microsoft.com/en-us/library/bb311040.aspx
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 am trying to convert a ASP.NET project to Entity framework. How to re-write the following query to its LINQ equivalent?
SELECT {Table1 objects}
FROM [Table1] tb1
INNER JOIN [Table2] tb2
ON tb1.Table1ID = tb2.fk_Table1ID
WHERE tb2.fk_attrib1 = '123' AND tb2.fk_attrb2 = '345'
ORDER BY tb1.attrib1
The result is a collection of Table1 objects.
Here Table1 and Table2 correspond to object System.Data.Objects.ObjectSet of ADO.NET Entity Framework.
var results = from tb1 in Context.Table1
join tb2 in Context.Table2 on tb1.Table1ID == tb2.fk_Table1ID
where tb2.fk_attrib1 == "123" && tb2.fk_attrb2 == "345"
orderby tb1.attrib1
select tb1;
Something like this:
context.Table1
.Where( o => o.Table2s.Any( o2 =>
o2.fk_attrib1 == '123' &&
o2.fk_attrib2 == '345' ) )
.OrderBy( o => o.attrib1 )
.ToList();
BTW, LINQPad is great for trying out L2E queries.
This should help you a little bit. I suppose the main problem is with JOIN clause - in EF you can use NavigationProperties and don't need to worry about joining tables - EF will take care of that for you.
Also you are trying to filter on column from joined table. This you can do using Any method to find all Table1 elements that are connected to Table2 where those referenced elements have certain properties/columns. You should also get familiar with All method, as it might be useful to you in future.
from t1 in context.Table1
where t1.Table2s.Any(t2.fk_attrib1 == "123" && t2 => t2.fk_attrb2 == "345")
order by t1.attrib1
select t1;
Edit:
I assume that there is 1:n relationship between Table1 and Table2 which results in enumerable collection as NavigationProperty in Table1 objects.
Edit2:
Fixed error in code - didn't noticed that both attributes are from Table2 not Table1
Should be something like this:
var result = (from tb1 in Table1
from tb2 in Table2
where tb1.Key == tb2.Key &&
tb2.fk_attrib1 = '123' &&
tb2.fk_attrb2 = '345'
select ione).OrderBy(p=>p.attrib1);
Hope this helps.