I have datatables A and B. Table A has columns 1 and 2. Columns 1 and 2 are the primary key. Table B has columns 1, 2, 4. Columns 1 and 4 are the primary key. I want to update table B so that for every value where B.1 == A.1 I want to make it so that B.2 = A.2. Because 2 is not part of the primary key for table B there may be multiple records where B.1 and B.2 are the same and I want to update 2 for all those rows.
I am stuck at this kind of code:
foreach(DataRow dr in A.Rows){
DataRow Found = B.Rows.Find(dr[1]);
if(Found != null)
Found[2] = dr[2];
}
The major problem I am facing is that because table B has a compound primary key that is shared by table A. The find is looking for two values but only one can come from table A.
Related
I have 4 columns that act as foreign keys and all are connected to one table which is a detail table for it. I have tried everything and can't get it to work. Instead of saying 1 it should say stefa. Those are names which all have their id's in the other table. I am doing it all in Windows forms the code and query builder, after I run it
SELECT
RadniDan.ID, RadniDan.Datum, RadniDan.PrvaSmena, RadniDan.DrugaSmena,
RadniDan.IspomocPrva, RadniDan.IspomocDruga
FROM
RadniDan
INNER JOIN
Radnik ON RadniDan.PrvaSmena = Radnik.ID
AND RadniDan.DrugaSmena = Radnik.ID
AND RadniDan.IspomocPrva = Radnik.ID
AND RadniDan.IspomocDruga = Radnik.ID)
WHERE
(RadniDan.Datum = ?)
I have two DataTables and I want to select the rows from the first one which are not present in second one, both tables have 3 Keys custnum, shiptonum, connum
For example:
Table Contacts
custnum shiptonum connum column
1 1 1 data1
2 2 2 data2
3 3 3 data3
4 4 4 data4
Table Invitations
custnum shiptonum connum column
1 1 1 data11
3 3 3 data33
I'd like the result to be:
Table Result
custnum shiptonum connum column
2 2 2 data2
4 4 4 data4
I already tried using
var differences = table1.AsEnumerable().Except(table2.AsEnumerable(),DataRowComparer.Default);
but it didn't work. For example in my testing in Contacts table I have 14,389 records, in Invitations table I have two records that exist in Contacts table the count after using the abovesolution was 14,389 instead of 14,387 (removing the two records from Invitations table).
You wrote:
I want to select the rows from the first one which are not present in second one
From your example, I see that you don't want to select rows from the first table that are not rows in the second table, but that you only want to take the values of the keys into account:
I want to select all rows from tableA which have keys with values that are not keys from tableB
You didn't define your tables. They might be IQueryable, or IEnumerable, for your LINQ statements there is not a big difference. Try to avoid AsEnumerable, especially if your data source is in a different process, like a database management system. The other process is much more efficient in executing your query than your process. AsEnumerable transports all data from your other process to your process, which is a relatively slow process. Therefore as a rule: Only use AsEnumerable this if you really need to,
The second definition defines clearer what you want: apparently from tableB you only need the keys:
var keysTableB = tableB.Select(row => new
{
CustNum = row.custNum,
ShipToNum = row.shiptonum,
ConNum = row.connum,
});
In words: from every row in tableB make one new object of anonymous type with three properties: CustNum, ShipToNum and ConNum
Select uses lazy execution. No query is executed, only property Expression is changed.
Now you want to keep only the rows from tableA that have a key that is a member of sequence keysTableB: if you want to keep a subset of a sequence, use Where
var result = tableA.Where(row => keysTableB.Contains(new
{
CustNum = row.custNum,
ShipToNum = row.shiptonum,
Connum = row.connum,
}));
In words: from every row in tableB keep only those rows that have a key that is also in keysTableB, using value equality.
TODO: consider concatenating these two LINQ statements into one.I doubt whether this would improve performance. It surely will deteriorate readability of your code, and thus decreases changeability / maintenance / testability.
for (int i=0;i<table1.rows.count;i++)
{
var rowExists = from dr in table2.AsEnumerable()
where dr.Field<typeofcolumn>("colum_name")==table1.Rows[i]["column_name"]
select dr;
if(rowExists.ToList().Count==0)
{
//here u import row table1.rows[i] to new table
}
}
I'm stuck with this linq query.
I've this tables.
ID A B C D
1 some data
2 some other data
Then, For every record on that table I may have none or many rows
ID TableA_ID R
1 1 1
2 1 2
3 1 5
4 2 2
For example. Row 1 (some data) has 3 rows on table B.
I tried using
tableA.Include(x => x.tablebchilds.Where( d => d.R == 1)).ToList()
but it is not working. With many others varation.
The objective of this query is to return tableA.row #1 if I pass it 1 as value (value of R). Number <> 2 won't give any result.
Tables are linked on EF. So TableB.tableA_ID is Foreign key of tableA.ID
Edit #1
I tried the answers in the question marked as duplicated with no luck. Give that 2 tableA.rows if the user insert 1 as parameter, linq query should return Row #1, some data. If 2 is passed as parameter, nothing is return.
A working SQL statement is:
SELECT [TableA].* FROM [TableA] JOIN [TableB] ON [TableA].[Id] = [TableB].[TableA_Id] WHERE [TableB].[R] = 1
Thanks!
If you have database relationship properly configured this have to work.
tableA.Include(x => x.tableBChilds).Where(tableA => tableA.tableBChilds.Any(b => b.R== 1)).ToList();
I am having a issue using TransactionScope and a check constraint in SQL Server.
I want to insert into the table as such:
Col A | Col B
------------
Dave | 0
Fred | 1
The table has a check constraint that there must always be an entry in Col B with '0'. The first row is inserting fine but the second row fails the constraint.
command.CommandText = #"INSERT INTO MyTable (ColA, ColB) VALUES(#ColA, #ColB)";
foreach (var row in model.Rows)
{
command.Parameters["#ColA"].Value = model.ColA;
command.Parameters["#ColB"].Value = model.ColB;
command.ExecuteNonQuery();
}
The check constraint calls the following function
IF EXISTS (SELECT * FROM mytable WHERE ColB = 0) RETURN 1
RETURN 0
Could this be because the constraint is only looking at committed data and if so how can it be told to look at uncommitted data as well
I don't think Check Constraints are suitable for a scenario like yours.You should use a instead of update/insert trigger to check that there's at least one row (in the table and /or in inserted values)
You have a inserted table in a trigger that contains all the rows that will be inserted so you can write something like this :
IF NOT EXISTS (SELECT * FROM mytable a UNION inserted WHERE ColB = 0) RIASEERROR("At least one row with ColB=0 should exist")
I'm using ADO.NET in a simple C# application.
There are two tables, TableA and TableB. TableA is a parent of Table B. TableA contains:
id as a primary key (Int32)
Other columns. I think it's irrelevant so I won't elaborate.
Table B has these columns:
id (primary key) (Int32)
tableAid (foreign key relationship with table A) (Int32 and primary key)
X (double type)
Y (double type)
I have created approximately 300 rows in table B. I want to update the columns values for X and Y to have the same value for each row. I'm currently doing this:
TableBRow[] rowsOfB = TableA.GetTableBRows();
for (int i = 0 ; i < rowsOfB.Length ; i++)
{
rowsOfB[i].X = newXvalue;
rowsOfB[i].Y = newYvalue;
}
This code seems to take a long time to run. My questions are (i) why is this slow ? and (ii) what is the recommended way of updating many rows in a table? Is there a bulk update approach.
if you're doing a large update, generally creating a temp table, doing a bulk insert, join on this temp table and do your update is quicker than doing n updates.