merge 2 different datatable using DataRelation - c#

I tried to merge 2 datatables like this. But my tables have different schema and different amount of rows. So I get an error here:
DataRelation drel = new DataRelation("EquiJoin",cr1, cr2, true);
dataSet.Relations.Add(drel);//error
As far as I understand, because first table has much more rows than second table.
Error: Cannot evaluate expression because a native frame is on top of the call stack.
Tables look like:
snowFlake: snow:
f_text | f_link_id | f_tabkey | | f_text | f_link_id | f_tabkey |
--------+---------------+-------------| |---------+---------------+-------------|
row1 | 100001 | 1 | | - | 100000 | 1 |
row2 | 100001 | 2 | | + | 100001 | 1 |
row3 | 100001 | 3 | | - | 100001 | 2 |
row4 | 100002 | 1 | | + | 100001 | 3 |
row5 | 100003 | 1 | | + | 100002 | 1 |
| + | 100003 | 1 |
| - | 100003 | 1 |
| + | 100004 | 1 |
| - | 100005 | 1 |
Primary keys that I use:
snowFlake.PrimaryKey = new DataColumn [] { snowFlake.Columns[IndexesField.F_LINK_ID], snowFlake.Columns[IndexesField.F_TABKEY] };
snow.PrimaryKey = new DataColumn [] { snow.Columns[IndexesField.F_LINK_ID], snow.Columns[IndexesField.F_TABKEY] };
DataColumn[] cr1 = { snowFlake.Columns[IndexesField.F_LINK_ID], snowFlake.Columns[IndexesField.F_TABKEY] };
DataColumn[] cr2 = { snow.Columns[IndexesField.F_LINK_ID], snow.Columns[IndexesField.F_TABKEY] };
How can I solve it?
Regards, Alexander.

Doesn't matter if they do not have same number of rows, because they are joined over a relation thus mimicking TSQL's Inner Join behavior. Without telling us the specific error, one can only assume that you haven't properly defined keys for joining (for example, the columns you specified in the relation don't exist).

Related

How can I add different examples with different columns in specflow examples

The following code is my Scenario:
Scenario: TradeOrders
Given Order 'SellOrder' Has Been Registerd
| Side | Price | Amount | IsFillAndKill | ExpireTime |
| 0 | 100 | 5 | false | 2024-02-05 09:30:26.2080000 |
And Order 'BuyOrder' Has Been Defined
| Side | Price | Amount | IsFillAndKill | ExpireTime |
| <Buy> | <Price> | <Amount> | <IsFillAndKill> | <ExpireTime> |
When I Register The Order 'BuyOrder'
Then The following 'Trade' will be created
| BuyOrderId | SellOrderId | Amount | Price |
| <BuyOrderId> | <SellOrderId> | <TradeAmount> | <TradePrice> |
And BuyOrder 'BuyOrder' Should Be Modified like this
| Side | Price | Amount | IsFillAndKill | ExpireTime |
| 1 | 100 | 0 | false | 2024-02-05 09:30:26.2080000 |
Examples:
| Buy | Price | Amount | IsFillAndKill | ExpireTime |
| 1 | 100 | 5 | false | 2024-02-05 09:30:26.2080000 |
Examples:
| <BuyOrderId> | <SellOrderId> | <TradeAmount> | <TradePrice> |
| 1 | 2 | 5 | 100 |
I want to add some examples with different columns but I have faced with below error:
Severity Code Description Project File Line Suppression State
Error Generation error: Message: The example sets must provide the
same parameters.
I appreciate you in advance.
This is not supported in SpecFlow. Since both tables are used in the same scenario, they need to go in the same row. It makes for a wide table, but there isn't anything you can do about it. Second of all, you cannot have < or > tokens in the column headers.
Examples:
| Buy | Price | Amount | IsFillAndKill | ExpireTime | BuyOrderId | SellOrderId | TradeAmount | TradePrice |
| 1 | 100 | 5 | false | 2024-02-05 09:30:26.2080000 | 1 | 2 | 5 | 100 |
You can also put spaces in the column headers so they are easier to read:
Examples:
| Buy | Price | Amount | Is Fill And Kill | Expire Time | Buy Order Id | Sell Order Id | Trade Amount | Trade Price |
| 1 | 100 | 5 | false | 2024-02-05 09:30:26.2080000 | 1 | 2 | 5 | 100 |
The choice to include spaces in column headers is subjective. It is supported to aid readability, but feel free to omit spaces if this does not make the examples table easier to understand.

Convert T-sql to Linq or Entityframework and not to use SqlQuery() for raw queries

I have a table which is called Signer :
+--------------+----------+---------+--------+--------------+---------+-----------+
| Name | User | Order | Signed | CompanyName | Status | InvoiceId |
+--------------+----------+---------+--------+--------------+---------+-----------+
| Anders | aa | 1 | 0 | OvnAnd2 | 0 | 26650 |
| Peyman | pm | 2 | 1 | OvnAnd2 | 1 | 26650 |
| Siw Ericsson | se | 3 | 0 | OvnAnd2 | 0 | 26650 |
| test | test | 4 | 0 | OvnAnd2 | 0 | 26650 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 26652 |
| test | test | 2 | 1 | OvnAnd2 | 0 | 26652 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 25365 |
+--------------+----------+---------+--------+--------------+---------+-----------+
Goal:
As you can see I have 3 different InvoiceId's. For each InvoiceId, I would like to find a row with minimum order number that Status column's value is 0 and User column has to be se.
( It means, Show the current users related invoices which are ready to be signed based on his/her username, order, signed columns)
I came up with this T-SQL which works fine :
select * from Signer s1
where s1.User = 'se' and Order = (select min(Order) from Signer s2 where s2.InvoiceId = s1.InvoiceId and Signed = 0)
And the result:
+--------------+----------+---------+--------+--------------+---------+-----------+
| Name | User | Order | Signed | CompanyName | Status | InvoiceId |
+--------------+----------+---------+--------+--------------+---------+-----------+
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 26652 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 25365 |
+--------------+----------+---------+--------+--------------+---------+-----------+
I would like to convert this query from T-Sql to Linq or EntityFramework :
This is my solution:
var temp = from x in db.Signers
where x.User == Me.UserName &&
x.Signed == 0
group x by x.InvoiceId
into item
select new
{
item.Key,
item = item.Min(x => x.Order)
};
Which returns 3 rows which is wrong because Siw should see the related invoices that are ready to be signed by her. (It means the first row should not be in the list)
+--------------+----------+---------+--------+--------------+---------+-----------+
| Name | User | Order | Signed | CompanyName | Status | InvoiceId |
+--------------+----------+---------+--------+--------------+---------+-----------+
| Anders | aa | 1 | 0 | OvnAnd2 | 0 | 26650 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 26652 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 25365 |
+--------------+----------+---------+--------+--------------+---------+-----------+
More info:
- As you can see in the first table, we have a special logic that someone can sign invoices out of order and Peyman is one of them.
- I don't want to use SqlQuery() method in Entityframework in order to execute t-sql queries.
I appreciate any help to find a solution for my goal.
Best regards
I think this is one of those situations where "let" come in handy:
var result = from s in Signers
let minToSign = Signers.Where(si =>
si.InvoiceId == s.InvoiceId && si.Signed == 0
).Min(si => si.Order)
where s.User == "se" && s.Order == minToSign
select s;

How to join Join two DataRows into a single DataRow through Column Name?

I am trying to figure out how to join two respective datarows into single datarow in dataset through Department column Name.
In provided dataset output i want to join Gastroenterology and Medical Gastroen(two datrows) through column name to single datarow (similar to Required Final dataset Output with Merged Rows).
Need Your ideas/help how it can be accomplished in asp.net and/or C#.
DataSet Output
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 15 25 40
General Medicine 25 05 30
Medical Gastroen 30 20 50
Required Final Dataset Output with Merged Rows
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 45 45 90
General Medicine 25 05 30
I think you must use joining for this in your database query. that would be better.
Table A
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table B.
+-----+---------------------+-------------+--------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
SQL QUERY
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Resulted Table:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
I hope it will help you.
You can Do something like this.
DataTable _dataTable = new DataTable();
DataRow _dataRow1 = null;
_dataTable.TableName = "Products";
_dataTable.Columns.Add("ID",typeof(int));
_dataTable.Columns[0].AutoIncrementSeed = 1;
_dataTable.Columns[0].AutoIncrement = true;
_dataTable.Columns.Add("ProductsName");
_dataTable.Columns.Add("Price");
_dataRow1 = _dataTable.NewRow();
_dataRow1["ProductsName"] = "Sony Laptop";
_dataRow1["Price"] = "15000";
_dataTable.Rows.Add(_dataRow1);
DataRow _dataRow2 = null;
_dataRow2 = _dataTable.NewRow();
_dataTable.Rows.Add(_dataRow2);
_dataRow2["ProductsName"] = "LG Laptop";
_dataRow2["Price"] = "15000";
DataSet _dataSet = new DataSet();
_dataSet.Tables.Add(_dataTable);

Recursive function to split list into list [duplicate]

This question already has answers here:
Split a list into sublist by checking a condition on elements
(3 answers)
Closed 8 years ago.
I'm trying to solve the following problem, I have a Collection that contain some data:
+-------------+---------------+-------------+
| Date | InitialValue | FinalValue |
+-------------+---------------+-------------+
| 21.05.2003 | 0 | 382087.14 |
| 23.06.2003 | 408206.52 | 110622.79 |
| 19.07.2004 | 123811.34 | 0 |
| 31.12.2011 | 0 | 0 |
| 08.06.2012 | 0 | 501854.71 |
| 31.12.2012 | 501854.71 | 546208.19 |
| 31.12.2013 | 634535.58 | 666284.47 |
| 30.06.2014 | 666284.47 | 725837.32 |
| 08.07.2014 | 725837.32 | 729646.48 |
+-------------+---------------+-------------+
What I need to do is to split this list into multiple lists when the final value is equal to 0. The expected result should be something like this:
Result list 1
+-------------+---------------+-------------+
| Date | InitialValue | FinalValue |
+-------------+---------------+-------------+
| 21.05.2003 | 0 | 382087.14 |
| 23.06.2003 | 408206.52 | 110622.79 |
| 19.07.2004 | 123811.34 | 0 |
+-------------+---------------+-------------+
Result list 2
+-------------+---------------+-------------+
| Date | InitialValue | FinalValue |
+-------------+---------------+-------------+
| 31.12.2011 | 0 | 0 |
+-------------+---------------+-------------+
Result list 3
+-------------+---------------+-------------+
| Date | InitialValue | FinalValue |
+-------------+---------------+-------------+
| 08.06.2012 | 0 | 501854.71 |
| 31.12.2012 | 501854.71 | 546208.19 |
| 31.12.2013 | 634535.58 | 666284.47 |
| 30.06.2014 | 666284.47 | 725837.32 |
| 08.07.2014 | 725837.32 | 729646.48 |
+-------------+---------------+-------------+
Can somebody give me an elegant way to solve my problem?
You don't always need linq:
var List<Data> myData;
var List<List<Data>> mySplittedData;
mySplittedData.Add(new List<Data>());
foreach(var item in myData)
{
mySplittedData.Last().Add(item);
if(item.FinalValue == 0)
mySplittedData.Add(new List<Data>());
}
Should work although I'm sure someone will come up with a clever one line linq solution :)
I'm not sure if this is elegant, but it uses Linq all the same:
var result = items.Aggregate(new List<List<Item>> { new List<Item>() }, (list, value) =>
{
list.Last().Add(value);
if (value.FinalValue == 0)
{
list.Add(new List<Item>());
}
return list;
});
Psuedo code
while (more result) {
list = input.Skip(n).TakeWhile(not 0)
n += list.Count()
use or store list
}
Not the most elegant, as the skip will have to skip over the first element of input many times, but might get you started.

MySQL Query to retrieve data from two tables even no data found and unique column in both tables

I have two tables.
Menu
+----------------+----------------+
| menu_id | menu_desc |
+----------------+----------------+
| 1 | menu1 |
| 2 | menu2 |
| 3 | menu3 |
| 4 | menu4 |
| 5 | menu5 |
+----------------+----------------+
Rights
+----------+--------------+---------+
| Role_id | menu_id | Rights |
+----------+--------------+---------+
| 1 | 1 | 3 |
| 1 | 2 | 3 |
| 1 | 3 | 3 |
+----------+--------------+---------+
I want the output something like this,
+----------+------------------+------------+-----------+
| menu_id | menu_desc | Role_id | Rights |
+----------+------------------+------------+-----------+
| 1 | menu1 | 1 | 3 |
| 2 | menu2 | 1 | 3 |
| 3 | menu3 | 1 | 3 |
| 4 | menu4 | 1 | null |
| 5 | menu5 | 1 | null |
+----------+------------------+------------+-----------+
Is it possible?
This gives NULL for both Role_id and Rights
SELECT Menu.menu_id, Menu.menu_desc, Role.Role_id, Role.Rights
FROM Menu LEFT OUTER JOIN Role ON Menu.menu_id=Role.menu_id
ORDER BY Menu.menu_id
try below, if not getting required results then share problem:
SELECT m.menu_id, m.menu_desc, r.Role_id, r. Rights FROM Menu m LEFT JOIN `Rights` r ON m.menu_id=r.menu_id order by m.menu_id;
You have to use join:
select Menu.menu_id, menu_desc, Role_id, Rights.menu_id, Rights.Rights from Menu join Rights on Menu.menu_id=Rights.menu_id;

Categories

Resources