score:1
This is an odd structure to map. Structurally it looks like a 1-to-0..1, but in concept it looks like it should be 1-to-many. For a 1-to-many I'd be expecting a table structure more like:
Applicant ( ApplicantId | Name )
ApplicantNote ( ApplicantNoteId | ApplicantId | Note )
This would be mapped in EF something like:
public class Applicant
{
public int ApplicantId { get; set; }
public string Name { get; set; }
public virtual ICollection<ApplicantNote> { get; set; } = new List<ApplicantNote>();
}
public class ApplicantNote
{
public int ApplicantNoteId { get; set; }
public virtual Applicant Applicant { get; set; }
}
public class ApplicantConfig : EntityTypeConfiguration<Applicant>
{
public ApplicantConfig()
{
ToTable("Applicant");
HasKey(x => x.ApplicantId);
HasMany(x => x.ApplicantNotes)
.WithRequired(x => x.Applicant)
.Map(x => x.MapKey("ApplicantId"));
}
}
public class ApplicantNoteConfig : EntityTypeConfiguration<ApplicantNote>
{
public ApplicantNoteConfig()
{
ToTable("ApplicantNote");
HasKey(x => x.ApplicantNoteId);
}
}
What you have is more like an Applicant table that contains a note, but then there is an additional table that can hold a single, additional extra note.
Applicant ( ApplicantId | Name | Note )
ExtraApplicantNote ( ApplicantId | Note ) // Name isn't required.
which in a 1-to-0..1 would look something like:
public class Applicant
{
public int ApplicantId { get; set; }
public string Name { get; set; }
public string Note { get; set; }
public ExtraApplicantNote ExtraApplicantNote { get; set; }
}
public class ExtraApplicantNote
{
public int ApplicantId { get; set; }
public string Note { get; set; }
public virtual Applicant Applicant { get; set; }
}
public class ApplicantConfig : EntityTypeConfiguration<Applicant>
{
public ApplicantConfig()
{
ToTable("Applicant");
HasKey(x => x.ApplicantId);
HasOptional(x => x.ExtraApplicantNote)
.WithRequired(x => x.Applicant);
}
}
public class ExtraApplicantNoteConfig : EntityTypeConfiguration<ExtraApplicantNote>
{
public ExtraApplicantNoteConfig()
{
ToTable("ExtraApplicantNote");
HasKey(x => x.ApplicantId);
}
}
This joins this extra applicant note record to the Applicant as an optional associated entity. When selecting as an entity graph:
var applicant = context.Applicants
.Include(x => x.ExtraApplicantNote)
.Single(x => x.ApplicantId == applicantId);
for example... then access the note(s) via applicant.Note
and applicant?.ExtraApplicantNote.Note
to account for the fact that an extra applicant note is optional.
To produce an output of all notes with their applicant details, a 1-to-many structure is far, far simpler to produce:
var notes = context.ApplicantNotes.Select(x => new
{
x.Applicant.ApplicantId,
x.Applicant.Name,
x.Note
}).ToList();
To do the same thing with a 1-to-0..1 is a fair bit more involved:
var notes = context.Applicants.Select(x => new
{
x.ApplicantId,
x.Name,
x.Note
}).Union(context.ExtraApplicantNotes.Select(x => new
{
x.ApplicantId,
x.Applicant.Name,
x.Note
})).ToList();
This involves first pulling the notes from the first table, then using a union to join the same details from the optional records in the second table.
** Edit ** Sorry, I re-read the question and you want the 2nd table to override the first.
In this case, similar to above:
var notes = context.ExtraApplicantNotes.Select(x => new
{
x.ApplicantId,
x.Applicant.Name,
x.Note
}).Union(context.Applicants
.Where(x => x.ExtraApplicant == null)
.Select(x => new
{
x.ApplicantId,
x.Name,
x.Note
})).ToList();
score:1
I would go for an inner join with .Join()
:
var lst = applicantList.Join(GetNewNotes,
(a) => a.APPLICANT_ID,
(n) => n.APPLICANT_ID,
(a, n) => return new
{
a.APPLICANT_ID,
a.Applicant_Name,
n.Notes
});
/*
lst:
2 | BEN TULFO | NoteA,
3 | ERNIE BARON | NoteB
*/
As a side note, is there any reason your second table contains ApplicantName
?
Why not keep this in Applicant table only?
EDIT:
After re-reading the question, I realized that you need the unmatched entries from the left list
too. So, that should be left outer join
instead, which you achieve with .GroupJoin()
and .SelectMany()
:
var lst = applicantList.GroupJoin(GetNewNotes,
(a) => a.Id,
(n) => n.Id,
(a, n) => new
{
Id = a.Id,
Name = a.Name,
Notes = a.Notes,
ApplicantNotes = n
})
.SelectMany(
g => g.ApplicantNotes.DefaultIfEmpty(),
(g, applicantNotes) => new
{
Id = g.Id,
Name = g.Name,
Notes = applicantNotes?.Notes ?? g.Notes
});
/*
lst:
1 | RAY HEAVENS | Note1
2 | BEN TULFO | NoteA
3 | ERNIE BARON | NoteB
4 | SUPERMAN | Note4
5 | MARK LAPID | Note5
*/
Source: stackoverflow.com
Related Articles
- Using Join to get the updated Table Linq Entity Framework
- How do I do a table join using LINQ and entity framework 6?
- How sum a column in Entity Framework using linq and join it with another table
- LINQ table join with entity Framework
- Left join after a into group in Linq using entity framework (core)
- How can I implement a LEFT OUTER JOIN in LINQ using lambda syntax on Entity Framework Core 2.0?
- Multi table join using Entity framework 4.1, should I use lambda or LINQ?
- Entity Framework + Linq LEFT JOIN using a where clause?
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- Can't update my db table using Entity Framework and LINQ
- Is there a Linq Entity Framework query for using an inner join AND an IN() statement?
- How to convert a SQL with multiple left joins to an Entity Framework LINQ statement using Include methods (No Join methods)?
- Performing join using linq query in Entity Framework
- Get columns values in data table using linq from Entity Framework
- Join into with column and variable value compared using Linq or Entity Framework queries
- Linq Join Data Table to Entity Framework - Not Evaluating results
- How to join one row to every row in source table using LINQ
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- Linq join does not include data from child table in Entity Framework
- Entity Framework Core: join two tables and get the properties I want using LINQ
- how to get two table combine record using entity framework with linq c#?
- Making an outer join on two anonymous type lists using LINQ in Entity Framework
- how to get a list of item in object from another table using linq and entity framework in C#?
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- c# WPF bind combobox to TPH in Entity Framework code first using LINQ
- Entity Framework and LINQ left join and include on same table
- Entity Framework - Database table Join in Linq to Entities
- Creating a join using a link table and the Entity Framework
- C#, LInq, Entity Framework 4: Split a table in to a two dimensional array using Linq
- Displaying equivalent to SQL pivot table using Linq against Entity Framework
- Linq-to-sql One-To-Many with a max
- Using the "let" kewword in a LINQ Query with EF 4.3
- ASP.NET Core 3.1 / EF Core - Search table column by string name
- Tricky string transformation (hopefully) in LINQ
- LINQ query to select object and count
- Select items from multiple lists via linq
- Entity Framework Error Deleting Objects With Foreign Keys
- Use contains in LINQ to SQL join
- Intersect two object lists on a common property and then compare a different property
- What is a good name for the following linq extension method?
- LINQ to XML binding to asp.Net DropDownList
- Simple LinkButton Postback
- How to return a list of anonymous object in Linq to Sql on joining two tables
- how to store dynamically the desired value in my linqued query variable
- "An attempt was made to load a program with an incorrect format"
- Linq to XML single or default
- how to display the data in grid view using linq query in web form
- Create TreeNodes with key in a Linq expression
- How to construct a raw SQL Query in EF Core with dynamic number of parameters
- Manipulating data in a collection with LINQ