I am trying to write an api which does data comparison. I get 2 tables (of same schema) dynamically. User will also pass primary key which supports multi column PrimaryKey.
I need to do Row comparison and only return extra rows in first table.
As of now
I could check if schema is matching or not and through exception if both are not matching,
Check all the columns in PrimaryKey (DataColumn[] type) and if any columns are missing then throw exception.
Then to avoid duplicate rows I am setting the DataTable PrimaryKey with user passed PrimaryKey in try catch and throwing exception in case it fails (This fails if there are any duplicates Pks in the table)
Now got stuck at A-B. I could write simple SQL which does the job for me:
Select A.Id, A.Name
From SampleDB..Emp1 A Left Outer Join SampleDB..Emp2 B On A.Id = B.Id
Where B.Id is null
A.Id = B.Id is dynamic in my case. If it is multi Column primary key I need to make this part dynamic.
I got an implementation which is kind of hardcoding which will not work in my case
Here is the code:
var tablesJoinend = from t1 in MasterSource.Rows.Cast<DataRow>()
join t2 in TargetSource.Rows.Cast<DataRow>() on t1["yourcolumn"] equals t2["yourcolumn"]
select t1;
DataTable output = tablesJoinend.CopyToDataTable();
in this join condition becomes hardcoded. For my case, I need to make it dynamic based on primaryKey
Any suggestion on how to do it using c#?
Related
I have table A, B, C,D.
Table D have forgienKey relationship columns(A1,B1,C1) with table A, B, C and Column C1 in table D is nullable column, because of it While loading table D using Include() nullable column row got skipped as below
_context.Entry(F).Collection(x=>x.D).Query().Include(x=>x.A).Include(x=>x.B).Include(x=>x.C).Load();
I could able to get value for Id's 1,3,4 using above Query but full row ID 2, 5 is not in the collection list because of the null value.
If C1 has null value I should get remaining column information. Help needed plz
The most obvious reason for me, why your data for some of the C columns are not coming is because the Include method which you are using, underneath is acting as join operations between tables, and as you know "join" will not bring results for two tables if they don't match with PK->FK relations.
One of the suggestions here is to call the SQL Stored Procedure.
Another suggestion is to use a kind of similar approach to LEFT OUTER JOIN using LINQ.
Here is an excellentemphasized text post about how to do that.
I have two MySql tables which I need to select a column from one, and where the results are used to select from another table. I know how to do it as two different select statements. However, I believe I can do it as a single statement but have no idea how.
Table one has two columns the second column has values which are also found in table two. I need to select all rows in table two which has the same values as those found in table one and where another column value is 0.
Any ideas how to go about doing this?
Use Join On tables to get columns form both table using query as
SELECT column_list
FROM table_1
LEFT JOIN table_2 ON
table_1.column = table_2.column;
Try to use Join query
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
I'm trying to compare two tables. I'd like to find out which rows exist in table1, and don't exist in table2. oData and oDataiSeries are DataTables, and properly populated.
var testing = from table1 in oData.AsEnumerable()
join table2 in oDataiSeries.AsEnumerable() on table1.Field<string>("SLOT") equals table2.Field<string>("SLOT")
where table1.Field<string>("SLOT") != table2.Field<string>("SLOT")
select table1;
testing ends up being null after the code runs. But if I were to do the following.
var testingtable1 = from table1 in oData.AsEnumerable()
select table1;
var testingtable2 = from table2 in oDataiSeries.AsEnumerable()
select table2;
testingtable1 and testingtable2 will be populated.
Is it because I'm trying to compare strings?
in testingtable1 and testingtable2, the column "SLOT" is String
Part of the problem:
I'm using VS 2015, and while debugging (hover mouse over DataTable object that should contain results), the Table isn't showing the proper results. It's showing the value of oData, instead of the comparison between oData and oDataiSeries. I'm not sure if I'm just misunderstanding how VS2015 displays var tables, or if this is a bug. If I bind the results DataTable to a gridview object, it'll display different rows as compared to the debugger. I ended up using #D Stanley solution, with a bit of modification.
I'd like to find out which rows exist in table1, and don't exist in table2.
You don't need a join for that. Just look for records that don't have a match in the second table:
var orphans = oData.AsEnumerable()
.Where(d => !oDataiSeries.AsEnumerable()
.Any(s => s.Field<string>("SLOT").Trim() == d.Field<string>("SLOT").Trim()));
or if you want to extract the values first to possibly improve performance, try
var series = oDataiSeries.AsEmumerable()
.Select(s => s.Field<string>("SLOT"));
var orphans = oData.AsEmumerable()
.Where(d => !oDataiSeries.Contains(d.Field<string>("SLOT"));
Is it because I'm trying to compare strings?
No, it's because join is an inner join by default, so ALL records will have matching values between the two datasets.
I have 2 tables
TableA:
TableAID int,
Col1 varchar(8)
TableB:
TableBID int
Col1 char(8),
Col2 varchar(40)
When I run a SQL query on the 2 tables it returns the following number of rows
SELECT * FROM tableA (7200 rows)
select * FROM tableB (28030 rows)
When joined on col1 and selects the data it returns the following number of rows
select DISTINCT a.Col1,b.Col2 FROM tableA a
join tableB b on a.Col1=b.Col1 (6578 rows)
The above 2 tables on different databases so I created 2 EF models and retried the data separately and tried to join them in the code using linq with the following function. Surprisingly it returns 2886 records instead of 6578 records. Am I doing something wrong?
The individual lists seems to return the correct data but when I join them SQL query and linq query differs in the number of records.
Any help on this greatly appreciated.
// This function is returning 2886 records
public List<tableC_POCO_Object> Get_TableC()
{
IEnumerable<tableC_POCO_Object> result = null;
List<TableA> tableA_POCO_Object = Get_TableA(); // Returns 7200 records
List<TableB> tableB_POCO_Object = Get_TableB(); // Returns 28030 records
result = from tbla in tableA_POCO_Object
join tblb in tableB_POCO_Object on tbla.Col1 equals tblb.Col1
select new tableC_POCO_Object
{
Col1 = tblb.Col1,
Col2 = tbla.Col2
};
return result.Distinct().ToList();
}
The problem lies in the fact that in your POCO world, you're trying to compare two strings using a straight comparison (meaning it's case-sensitive). That might work in the SQL world (unless of course you've enabled case-sensitivity), but doesn't quite work so well when you have "stringA" == "StringA". What you should do is normalize the join columns to be all upper or lower case:
join tblb in tableB_POCO_Object on tbla.Col1.ToUpper() equals tblb.Col1.ToUpper()
Join operator creates a lookup using the specified keys (starts with second collection) and joins the original table/collection back by checking the generated lookup, so if the hashes ever differ they will not join.
Point being, joining OBJECT collections on string data/properties is bad unless you normalize to the same cAsE. For LINQ to some DB provider, if the database is case-insensitive, then this won't matter, but it always matters in the CLR/L2O world.
Edit: Ahh, didn't realize it was CHAR(8) instead of VARCHAR(8), meaning it pads to 8 characters no matter what. In that case, tblb.Col1.Trim() will fix your issue. However, still keep this in mind when dealing with LINQ to Objects queries.
This might happen because you compare a VARCHAR and a CHAR column. In SQL, this depends on the settings of ANSI_PADDING on the sql server, while in C# the string values are read using the DataReader and compared using standard string functions.
Try tblb.Col1.Trim() in your LINQ statement.
As SPFiredrake correctly pointed out this can be caused by case sensitivity, but I also have to ask you why did you write your code in such a way, why not this way:
// This function is returning 2886 records
public List<tableC_POCO_Object> Get_TableC()
{
return from tbla in Get_TableA()
join tblb in Get_TableB() on tbla.Col1 equals tblb.Col1
select new tableC_POCO_Object
{
Col1 = tblb.Col1,
Col2 = tbla.Col2
}.Distinct().ToList();
}
where Get_TableA() and Get_TableB() return IEnumerable instead of List. You have to watch out for that, because when you convert to list the query will be executed instantly. You want to send a single query to the database server.
I have a SQL query that works perfectly that I need to convert to Linq. I need to return all the records one table and join it to a second table. I need to return all of the results from the first table joined with results from the second table where the value of a specific field in the second table equals a variable value (75 in the example below) or returns null.
So the total number of rows in the result should be the total number of rows from table1. The part of the row from the join from table2 should either show values from table2 where a record existed with a value of 75 or null where the record doesn't exist.
EDIT: I should mention that t1.field1 is an int and t2.field1 is a nullable int.
I tried multiple linq statements, grouping joins, asking coworkers, and googling til my eyes bleed. I'm stuck. I realize my question wording may not be clear, and I apologize in advance if it isn't.
Thanks in advance.
Chris
The SQL Query:
SELECT *
FROM table1 AS t1 LEFT OUTER JOIN
table2 AS t2 ON t1.field1 = t2.field1 AND t2.field2 = 75
Use DefaultIfEmpty - see LINQ - Left Join, Group By, and Count and http://msdn.microsoft.com/en-us/library/bb397895.aspx for samples of how to achieve this
An answer that works but isn't as elegant as I'd expected:
var q = from item1 in table1
join item2 in table2 on new { Field1=(int?)item1.Field1, Field2=75 }
equals new { item2.Field1, item2.Field2 } into gj
from subItem2 in gj.DefaultIfEmpty()
select new { F1= item1.Field1, F2 = ( subItem2 == null ? string.Empty : subItem2.Field2.ToString()) };
I couldn't see where to fit the where clause in (Field2 = 75) so instead went with the composite key between the tables to achieve the same effect.
Second bit of ugliness is the cast to the nullable int because or Field1 in the composite key so it can be equated to the corresponding field on table 2.
Obviously you return in the anonymous type whatever values you're interested in. Note you can't return null which is why I showed a string representation of Field2 just so you can see what is returned from the second table.
Thank you for your responses. Because I need some nulls, but only the ones where the specific id is null and not all values that are null, none of the solutions above will work. It seems that to do this in linq will be very tough if it is possible.
So, in the interest of time, I decided to take the SQL query that worked and turn it into a stored procedure and function import. I feel like that's probably not the right way to do it, but time is always a factor.
Thanks again for your responses.