Duplicate fields name in join result in Dapper + Oracle in C# - c#

I'm doing test in Dapper + Oracle in C#
the sample tables as below
Book
+----+----------------+-----------+
| id | title | author_id |
+----+----------------+-----------+
| 1 | this is a book | 2 |
+----+----------------+-----------+
Author
+----+------+
| id | name |
+----+------+
| 2 | Mark |
+----+------+
my code:
string queryString = "SELECT distinct * FROM book b INNER JOIN author a ON a.id = b.author_id";
var result = connection.Query(queryString);
the output:
{DapperRow, id = 1, title='this is a book', author_id = 2, id = 2, name = 'Mark'}
there are two fields with duplicate name "id" in result
how could I add alias or postfix to the duplicate fields like the output in sqldeveloper like below
+----+----------------+-----------+------+------+
| id | title | author_id | id_1 | name |
+----+----------------+-----------+------+------+
| 1 | this is a book | 2 | 2 | Mark |
+----+----------------+-----------+------+------+
thanks

Use aliases in select statement:
string queryString = "SELECT distinct b.id [book_id], ... FROM book b INNER JOIN author a ON a.id = b.author_id";

Related

How to Join or search Id with one column which have some Id

I have two tables:
Student Conference
+----+------+ +----+-------+---------------+
| id | name | | id | name | ref_studentId |
+----+------+ +----+-------+---------------+
| 1 | jack | | 1 | Rose | 1,12 |
| 2 | kai | | 2 | White | 12 |
| 3 | wind | | 3 | Black | 1,12,12356 |
+----+------+ +----+-------+---------------+
And I want to join them together with studentid or search for studentid in conference table.
You can use the LINQ Join method to achieve what you want:
var result = dataContext.Students.Join(dataContext.Conferences,
st => st.id,
cf => cf.ref_studentId,
(student, conference) => new { ... });
Although I would strongly recommend using the Entity Framework Navigation Properties, using which the above can be done more easily:
var result = dataContext.Student.Include(st => st.conference);
Update:
Please note that the above LINQ Query will fail to execute unless you will fix your Conference Table design, which is against SQL Normalization Forms (specifically the 1st Normal Form), saying that:
Each sell should be single-valued.
That means that you shouldn't have a comma (or any other character) separated values in your table columns.
To make the above query work, you have to make the ref_studentId contain only a single value for the StudentId:
Conference
+----+--------+-----------+
| ID | Name | StudentId |
+----+--------+-----------+
| 1 | Rose | 1 |
| 2 | Rose | 12 |
| 3 | White | 12 |
| 4 | Black | 1 |
| 5 | Black | 12 |
| 6 | Black | 12356 |
+----+--------+-----------+
But to be honest, this one too will not match the SQL Normalization rules.
Specifically, the 2nd Normal Form, saying that:
All Attributes (non-key columns) should be dependent on a Key.
And the 4th Normal form, saying that:
There should be no multi-value dependencies (Rose ⟶ 1, Rose ⟶ 12)
The correct solution would be to create another table for the Student⟶Conference relations.
Conference ConferencesStudents
+----+-------+ +----+--------------+-----------+
| ID | Name | | ID | ConferenceID | StudentId |
+----+-------+ +----+--------------+-----------+
| 1 | Rose | | 1 | 1 | 1 |
| 2 | White | | 2 | 1 | 12 |
| 3 | Black | | 3 | 2 | 12 |
+----+-------+ | 4 | 3 | 1 |
| 5 | 3 | 12 |
| 6 | 3 | 12356 |
+----+--------------+-----------+
Now, for this one the LINQ query will be:
dataContext.ConferencesStudents.Join(dataContext.Students,
cf => cf.ConferenceID,
st => st.ID,
(conferencesStudents, student) => new { conferencesStudents, student })
.Join(dataContext.Conferences,
cfSt => cfSt.conferencesStudents.ConferenceID,
cf => cf.ID,
(cfSt, conference) =>
new
{
Student = cfSt.student,
Conference = conference
});
Note: for the above, I've used the Anonymous Types just for demonstration and my strong advice would be to use real class Models instead.
OR
By using the same Navigation Properties (in case if you correctly had defined the EF relations) you can have a simpler version:
dataContext.ConferenceStudents.Include(cf => cf.Conferences)
.Include(cf => cf.Students)
Update 2:
I hate to say it but there is a kind of workaround in case if you can't change the Database design:
var results = (
from c in dataContext.Conference.ToList() // populating all Conferences
from s in dataContext.Students.ToList() // populating all Students
where c.ref_studentId.Split(',').Contains(s.id.ToString())
select new
{
Student = s,
Conference = c
}).ToList();
Note: this WILL NOT BE EFFECTIVE from the application performance perspective.
The better alternative compared to the above could be to write Stored Procedure and call it from the EF.
I have prepared a fiddle for you - if there are any questions feel free to ask.
Btw: This solution solves your question. The data-structure itself is ugly and should be normalized.
void Main()
{
var students = new List<Student>
{
new Student { Id = 1, Name = "jack" },
new Student { Id = 12, Name = "kai" },
new Student { Id = 12356, Name = "wind" }
};
var conferences = new List<Conference>
{
new Conference { Id = 1, Name = "Rose", RefIds = "1,12" },
new Conference { Id = 2, Name = "White", RefIds = "12" },
new Conference { Id = 25, Name = "Black", RefIds = "1,12,12356" }
};
var result = students.Select(s => new Tuple<int, Conference[]>(s.Id, conferences.Where(c => c.RefIds.Split(",").Contains(s.Id.ToString())).ToArray()));
}
// Define other methods, classes and namespaces here
public class Student
{
public int Id {get; set;}
public string Name {get;set;}
}
public class Conference
{
public int Id {get; set;}
public string Name {get; set;}
public string RefIds {get; set;}
}

Create a stored procedure to display rows with a connected column value from another column

I have this SQL Server database already stored
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
1 | 5374 | John | 11135
2 | 9864 | Doe | 7394
3 | 11135 | John | 21784
4 | 7394 | Doe | 6872
5 | 21784 | John | NULL
6 | 6872 | Doe | NULL
When I'm going to display ID 3
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
1 | 5374 | John | 11135
3 | 11135 | John | 21784
5 | 21784 | John | NULL
When I'm going to display RecordID 2
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
2 | 9864 | Doe | 7394
4 | 7394 | Doe | 6872
6 | 6872 | Doe | NULL
The display only stop when no PrevTaxDec connects TaxDec No value.
I got this solution given to me a while but I don't know how to convert it to stored procedure which the data is already inserted the table
DECLARE #ID int = 3;
WITH YourTable AS(
SELECT V.ID,
V.TaxDecNo,
V.PrevTaxDec
FROM (VALUES(1,5374,11135), --This value must already inserted in the table
(2,9864,7394),
(3,11135,21784),
(4,7394,6872),
(5,21784,NULL), --I assume you aren't really mixing datatyoes. 'N/A' can't be inserted into an int column
(6,6872,NULL))V(ID,TaxDecNo,PrevTaxDec)),
--Solution
rCTEup AS(
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM YourTable YT
WHERE YT.ID = #ID
UNION ALL
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM rCTEup r
JOIN YourTable YT ON r.TaxDecNo = YT.PrevTaxDec),
rCTEdown AS(
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM YourTable YT
WHERE YT.ID = #ID
UNION ALL
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM rCTEdown r
JOIN YourTable YT ON r.PrevTaxDec = YT.TaxDecNo)
SELECT ID,
TaxDecNo,
PrevTaxDec
FROM rCTEup
UNION ALL
SELECT ID,
TaxDecNo,
PrevTaxDec
FROM rCTEdown
WHERE ID != #ID; --As it'll be in rCTEup

Joining table to other table using either one of two columns

I'm currently writing a program in C# and I want to load friendships from the database based on the id passed as function parameter.
I got 2 tables (I only display the important columns).
Table 1: players
+----------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(15) | NO | | NULL | |
+----------------------+---------------------+------+-----+---------+----------------+
Table 2: messenger_friends
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| user_one_id | int(11) | NO | PRI | NULL | |
| user_two_id | int(11) | NO | PRI | NULL | |
+-------------+---------+------+-----+---------+-------+
The thing is, my idea was the following: in messenger_friends, save one line for a friendship. I know I can save 2 for one friendship but it would mean more storage as 500 friendships would become 1000 records. Now, in my application I have to JOIN messenger_friends to players. I got this function:
public async Task<IReadOnlyList<MessengerFriend>> GetFriends(int playerId)
In here, I need to get all records from messenger_friends where EITHER user_one_id OR user_two_id is playerId. Then in the same query, I want to join it to players. I know I can get the records this way:
SELECT * FROM messenger_friends WHERE user_one_id = {playerId} OR user_two_id = {playerId}
But I'm not sure how to join it to the players table as I need to join EITHER user_one_id OR user_two_id with players.id
SELECT *
FROM players p
JOIN messenger_friends f ON p.id in (f.user_one_id, f.user_two_id)
WHERE p.id = {playerId}
or
SELECT *
FROM players p
JOIN messenger_friends f ON p.id = f.user_one_id
OR P.id = f.user_two_id
WHERE p.id = {playerId}

Better way to insert data in table from other table with ADO.NET

I have two tables:
Table1 - MainTable
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
Table2 - BookTable
----------------------
| BookID | BookName |
----------------------
| 1 | physics |
----------------------
| 2 | Math |
----------------------
I want to get the result like this:
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
| 1 | Alex | Math |
---------------------------------------
I have list of BookNames in BookTable and I want to insert Data in MainTable. I am using ADO.NET entity data model in visual studio and I am doing this so:
BookTable correspondingBook=(from row in entities.BookTable
where rows.BookName == "Math"
select rows).First();
MainTable itemToAdd = new MainTable();
itemToAdd.CustomerName = "Alex";
itemToAdd.BookID = correspondingBook.BookID;
entities.MainTable.Add(itemToAdd);
entities.SaveChanges();
Is it good solution for this problem? if no, Which will be better?

LINQ join tables with value if no match

I know that there are some examples but I could not apply them on my code. I am pretty new to Linq and SQL. I have two tables I want to join.
First Table:
--------------
| Id | Count |
--------------
| 1 | 10 |
--------------
| 2 | 4 |
--------------
Second Table:
--------------
| Id | Name |
--------------
| 1 | Tom |
--------------
| 2 | John |
--------------
| 3 | Nick |
--------------
| 4 | Max |
--------------
As you can see, the second table has more records than the first. My goal is to join them based on the Id. The problem is that after I have joined the tables it only displays the matching records, which is Id 1 and 2. Though I want to display every Id (from 1 to 4) and if there is no match in both tables, there should be a default value 0.
It should look like this:
----------------------
| Id | Name | Count |
----------------------
| 1 | Tom | 10 |
----------------------
| 2 | John | 4 |
----------------------
| 3 | Nick | 0 |
----------------------
| 4 | Max | 0 |
----------------------
So far I have got this code:
// first table
var listCount = entity.tblKundes.Where(x => x.Studio == 2)
.Select(x => new { x.Id, x.Name})
.GroupBy(x => x.Name).ToList();
// second table
var listBerater = entity.tblUsers.Where(x => x.Studio == 2)
.Select(x => new { x.Id, x.Name})
.ToList();
// This join should be edited so that it displays non matching records as well
var test = listCount.Join(
listBerater,
count => count.Key,
berater => berater.Id,
(count, berater) => new { listCount = count, listBerater = berater }
).ToList();
Edit:
var test2 = (from list in listCount
join berater in listBerater on list.Berater equals berater.Id into gj
from sublist in gj.DefaultIfEmpty()
select new { sublist.Id, sublist.Nachname, sublist.Vorname }).ToList();
There is a typical concept in every Structured Query Languages which is called "Left join". Left-Join means that you will have all rows of data from first table even there is no equivalent in the second one. "Inner-Join" is a little different and only looks for matched rows of data.
Here you can find enough and complete information about your issue.
Left Join

Categories