Finding duplicates among two datatables [duplicate] - c#

This question already has answers here:
How to perform Dynamic Join using LINQ
(1 answer)
Linq Join with Dynamic Expression
(1 answer)
dynamic join in linq 0 c#
(2 answers)
Closed 5 years ago.
I stuck to find duplicates from 2 datatables where the condition is dynamic.
Let's say TABLEA and TABLEB has same schema
and comparison columns could be anything.
Columns could be more than one.
how can i build a dynamic condition to remove duplicate from TABLEA.
here i tried this query
e.g. i have column name with comma seperated
dynamiccolumnA, dynamiccolumnB, dynamiccolumnC
var matched = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on
table1.Field<object>(dynamiccolumn) equals table2.Field<object>(dynamiccolumn)
where table1.Field<object>(dynamiccolumn) != table2.Field<object>(dynamiccolumn)
select table1;
" where table1.Field(dynamiccolumn) != table2.Field(dynamiccolumn)" this statment could be for more than one column.
Could anybody gives me some pointer regarding this.
Thanks in advance

Looking at your problem, you need a query which can be customized without having the source code recompiled. I would suggest you to use expression trees. They are tough to understand and can bring complexity but its well worth the price.
Take a look at this article
Microsoft Article on Expression Trees

Related

How to match SQL Queries in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to match same type of SQL Queries with different condition values,
For example :
SELECT * FROM Customer Where Age > 20 AND Age < 40
SELECT * FROM Customer Where Age > 30 AND Age < 50
Both of the above queries are the same except the values in the WHERE condition (20, 40, 30 and 50). I want to identify such queries. It should work with HAVING as well. It should work for any value type in the condition (int, varchar, date etc).
Basically I want to write a C# function to which I can pass 2 queries and it should return true if both queries are the same except the values in the exclusion condition.
Another Example :
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 50;
SELECT Employees.FirstName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY FirstName
HAVING COUNT(Orders.OrderID) > 50;
When I pass 1st and 2nd queries it should return true, but false for 2nd and 3rd.
I tried with Regular Expressions, but how to find where the parameter located? It can be anywhere.
Is it possible to do it with SqlScriptDom? How? I am using SqlScriptDom to get the table names from the SQL query, but how to get parameters?
Okay, I don't mean to pick on your language but I think it's kind of important here. The queries in your example don't have parameters. They have exclusion criteria in a WHERE clause. It sounds like what you're trying to do is compare the text of two queries for everything except the WHERE clause. ANSI SQL and T-SQL both follow the same convention that in a SELECT query the WHERE clause comes after the FROM clause and before any GROUP BY, HAVING or ORDER BY clause. So you could pull it out and compare it that way if you were going to just analyze the text of the code. One issue you might think about though is that SQL often provides subtly different ways of accomplishing the same thing. For example, if in your examples, you had <= and >= instead of < and > you could use the BETWEEN operator.
I think you probably could use the SqlScriptDom to do what you want but I'm not good enough with that to help really.

accessing dataset tables [duplicate]

This question already has answers here:
How to get the identity of an inserted row?
(15 answers)
Closed 7 years ago.
I wonder what is the right approach for getting tables from dataset.
should i return the data within my procedure with select statement?
this is how i do it ,it works but i am not sure this is the right way.
SELECT SCOPE_IDENTITY()
and this is how i access my table:
ds.Tables[0].Rows[0][0]).ToString()
You might want to try using DataRow and looping though the set
foreach(DataRow row in ds.Tables[0])
{
string someData = row["keyname"].ToString();
}
I'm unsure of your question in the first place. But MSDN has a lot of ducumentation, and you can read up on more data structures that can help with DataTables and how to access them. And even query the DataTable with linq like this
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;

Getting a value from datatable [duplicate]

This question already has answers here:
Filling data at a particular location in a Data Table in C#
(3 answers)
Closed 8 years ago.
I have a Datatable like this.Now how can i pick the value from zones based on weight and zone.I am doing this in c#
Using LINQ Query : You need to use the AsEnumerable() extension for DataTable
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
AsEnumerable() returns IEnumerable. If you need to convert IEnumerable to a DataTable, use the CopyToDataTable()
Check Here
If you want to pick cells/rows with criteria. you need to a select clauase in your datatable. Go Here to see some examples

Using linq to select all tables in database [duplicate]

This question already has answers here:
Linq: Get a list of all tables within DataContext
(4 answers)
Closed 8 years ago.
I want to select list of all tables (Not columns) in a database using LINQ dynamically.
I just want it Dynamically not what listed in DataContext static values.For example i alter Table2 after deploying program. in this situation how i should find it.
By the way of there are any query also please let me know.
I think you can use the "Mapping" feature of LINQ:
context.Mapping.GetTables();
if you want to get tables that are modeled you can use #Mygyll answer, but if you want to list all tables in database you can use SMO, in smo when you have a database you can get all tables via this code
db.Tables.Cast<Table>()

How to make a select query on a many to many relation using linq? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Many to many relation linq query in EF
how is it possible to query a many to many relation with linq.
Explanation: i have two tables "conventions" and "participants" with many to many relation the problem is that i can't access the properties of "participants" within the query.
the query:
var conv1 =from c in db.Conventions
where c.objet.Contains(s)
|| c.Domaines.intituleDomaine.Contains(s)
/* snip */
select c;
and i need to join the other table "participants" and search inside it's properties.
Thank you in advance
check this http://www.codeproject.com/Articles/15437/Create-many-to-many-relationships-in-LINQ-for-SQL

Categories