How to Query 'In clause' upon collection of Collection in Linq - c#

I have a list of Ids and I want to fire a 'In Clause' query on nested collection.How would i do that.
In more detail I have 2 tables
Table : A
Table : B
These both tables have their respective primary keys,they are using another "table C" to store their mapping details.
Table : C
Table C contains two columns both are the primary keys of respective table A and Table B.
Now I have Ids from Table A, and get all the records from B which are associated with A using Table C. I want to fire an 'In Query' just like Sql in Linq but not be able to fire on collections of Table C.
I know in linq we have WhereIn clause as well. but its not showing in the list of it.
My query is something like that.
Context.B.Where(item=>item.C.WhereIn(item1=>Item1.AID==aid));
I want something like that query but with 'C' its not showing the wherein clause as well what should i do any suggestion.

Try this one:
// Select the ids of the table B that are associated with ids in aIds.
var bIds = Context.C.Where(x=>aIds.Contains(x.aId)).Select(x=>x.bId);
// Get the record of table B, which their ids are in bIds
var result = Context.B.Where(x=>bIds.Contains(x.Id));

Related

How to use Lambda in LINQ select without join between two tables or more

I've three tables
Investigators: id, name, first name
Articles: id, name, date
ArticlesInvestigators: id_Investigator, id_Articles, order
I want to get only all the investigators from the Investigators table to have one article published:
In SQL it would look like:
SELECT * FROM Investigators i, ArticlesInvestigators a
WHERE i.id = a.id_Articles
But how shall be in LINQ and lambda? Without join because only want the results of investigators table.
I don't know if i understand your question or not but try this way and tell us if it work or not :
1) you need to make FOREIGN KEY between your tables
2) do you query in C#
3) then you can get you data using the relation between your tables

Natural Join of two DataTables in C#

I have a DataSet that contains two tables. One is considered to be nested in the other.. All I want is for it to not be nested and for there to be one table. .Merge() and LINQ just aren't doing the trick.
Here is a sample of what the main table would look like
student-id ID
--------------------
123456789 1
654987321 2
But each of these has multiple rows that they correspond to in the next table
ID Col1 Col2 etc.
----------------------
1 fact1 fact2
1 fact3 fact4
2 fact5 fact6
I want to combine them so they would look like this...
student-id Col1 Col2
-------------------------------
123456789 fact1 fact2
123456789 fact3 fact4
654987321 fact5 fact6
Everytime that I try the merge it doesn't work I get an error that I cant duplicate the primary key which is "ID" and since the merge is based on the primary key(i believe) I cant remove it.
I cant use LINQ because I want to make this generic so that the second table could have any number of columns and I cant get the select to work for that.
UPDATE: MY SOLUTION
I ended up cloning the second table to a new data table. Then adding a column called 'student-id' and deleting the ID column. The I looped through the rows of the Main table finding and related them to row in the second table... Combined all the data in an array and created a row in the final table.
The LINQ isn't as bad as you suggest. You can just use an anonymous type that holds two DataRows:
var result = from t1 in table1.AsEnumerable()
join t2 in table2.AsEnumerable() on (int)t1["ID"] equals (int)t2["ID"]
select new
{
Student = t1,
Facts = t2
};
foreach(var s in result)
Console.WriteLine("{0} {1} {2}", s.Student["student-id"], s.Facts["Col1"], s.Facts["Col2"]);
That way, you're not including specific columns in your output, so you can access them after the fact.
That being said, the other poster's suggestion of using a pivot table is probably a better direction to go.
let's try it in SQL.
Let, 1st Table = Table1 and 2nd Table = Table2
SQL:
Select
X.student-id,Y.Col1,Y.Col2
From Table1 As X Inner Join Table2 As Y On Y.ID=X.ID
I think if you try it in SQL it's easy to do!!!
Sounds like what you need is a Pivot table.
This will essentially allow you to display the data how you want.
Here are a couple of tutorials/projects
http://www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET
http://www.codeproject.com/Articles/46486/Pivoting-DataTable-Simplified
Update
you may find yourself better doing the 'pivot' part in MS SQL as stored procedure and then populating your datatable with the results of calling this stored procedure. This example here is a great starting block
http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx

Sql Join query for treeview

Hi, I have 3 Tables in my database:
I want show the select Query from this table in treeView. I use RadTreeView, to show in treeview. I need to query by these Fields: DisplyMember | ParentMember | ChildMember.
How do I join theses tables for this query?
Your datasource would be your datagrid or list of whatever you are using to display the table. The display member would be whatever you wanted so say group name for this one. The parent member would be Groups. Value member would be groupid. I'm not real familiar with radtree view but I think this may help. Check this out too. It may help you http://www.telerik.com/help/winforms/treeview-data-binding-binding-to-object-relational-data.html

SQL: Joining tables on primary key AND returning the primary key

I'm writing a C# application and am using an Access .mdb. I have a table with email messages and a table with message relations (each email msg can be assigned to several teams of workers), so the rows in the relations table have "msgId" and "teamName" fields.
I want to to get all messages from the first table which are assigned to a specified team. I'm using the following query:
"SELECT * FROM Mails INNER JOIN MailAssignments ON Mails.msgId = MailAssignments.msgId"
But it doesn't return the msgId for me, I guess, because the tables are joined on this field, but then I
m not able to identify messages in my C# code.
How can I make the query return the msgId for me?
It is enough specify the fields name in the selection, or add the table name where you want to get all the fields, try with this selection list :
SELECT Mails.*
FROM Mails INNER JOIN MailAssignments
ON Mails.msgId = MailAssignments.msgId
It should appear twice in the resultset, for Mails.msgId and MailAssignments.msgId respectively. However, you should not expect two columns named msgId. Rather, the DBMS should disambiguate the columns. This is a requirement of Standards SQL, BTW.
IIRC Access will rename both columns in order to disambiguate them. This would explain why there is no column named msgId in the result.

Getting Value From Another Table with a Foreign Key

I've built a report with the report wizard in C#, and I can see my report in ReportViewer. My problem is that I have 2 tables that are related. The value of a column in the first table is a foreign key on the other table. All I see in that column is just the foreign key. I'd like to see the corresponding value from the other table.
How can I see value of the column from the second table?
You should just join on the foreign key columns:
SELECT
a.*,b.YourNeededColumnHere
FROM TableA a
INNER JOIN TableB b ON a.columnX=b.columnX
However, if you are having problems doing this in reporting services, just create a view:
CREATE VIEW CombinedAB
AS
SELECT
a.*,b.YourNeededColumnHere
FROM TableA a
INNER JOIN TableB b ON a.columnX=b.columnX
GO
you should now be able to run your report off the CombinedAB view as:
SELECT
*
FROM CombinedAB
WHERE ...your conditions here...
sounds like you probably need a drill down report
Create Basic Drilldown Report
Regarding the control that renders the result... Does it have its columns bound manually / is it set to auto-bind?
Change your query to one that involves the fields from the appropriate tables.
SELECT t1.*, t2.value
FROM table1 t1
JOIN table2 t2 ON t1.t2id = t2.id
But if you can't, because the data sources are separate, then you'll be wanting another option.
Assuming you have two datasets, from separate data sources.
Put a table in your report to show information from DataSet1 (or whatever it's called). Then put a rectangle in place of one of your textboxes, and then put a table in there, which you attach to DataSet2. Then put a filter on this table, so that it only shows records from DataSet2 which correspond to the appropriate value in DataSet1.
Or, wait for SQL Server 2008 R2 (currently in CTP), which provides a Lookup function for exactly this purpose.
Rob
Drag the item from the table where it's the Foreign Key, not it's native location.

Categories