LINQ Inner Join: Query / Method Syntax

Contenuti

LINQ Inner Join: Lo avete mai usato? Se per voi è una nuova esperienza sappiate che la INNER JOIN ha lo scopo di creare un nuovo set di dati legati dalla relazione specificata nella condizione.

Per fare l’esempio ho definito due classi minimali dal nome Film e FilmCategory dove il legame è dato da IDCategory. Film & FilmCategory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

public class Film
    {
        public string Title { get; set; }
        public int IDCategory { get; set; }
    }

    public class FilmCategory
    {
        public string Description { get; set; }
        public int IDCategory { get; set; }
    }

Creazione %22Fake List%22

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

_films = new List<Film>(){
                new Film() { IDCategory = 1, Title = "Film 1" },
                new Film() { IDCategory = 1, Title = "Film 2" },
                new Film() { IDCategory = 2, Title = "Film 3" },
                new Film() { IDCategory = 3, Title = "Film 4" }
            };

            _categories = new List<FilmCategory>(){
                new FilmCategory(){ IDCategory =1,Description="Category 1"},
                new FilmCategory(){ IDCategory =2,Description="Category 2"},
                new FilmCategory(){ IDCategory =3,Description="Category 3"},
                new FilmCategory(){ IDCategory =4,Description="Category 4"},
            };

Linq Inner Join Query Syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

var result = (from c in _categories
                          join f in _films
                              on c.IDCategory equals f.IDCategory
                          select new
                          {
                              FilmName = f.Title,
                              Category = c.Description
                          }
                         ).ToList();

LINQ Inner Join Method Syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

 var result = _categories.Join(_films,
                c => c.IDCategory,
                f => f.IDCategory,
                (c, f) => new
                {
                    ProductName = f.Title,
                    Category = c.Description
                }
            );

Come potete notare l’esempio (in entrambe le sintassi proposte) è molto semplice e questo permette di interrogare contemporaneamente più collezioni di oggetti legate tra di loro.

Ora - prima di chiudere- ecco l’output prodotto dalla JOIN creata in precedenza. Per mostrarlo ho esportato in JSON (vedi JsonConvert: Newtonsoft From object to Json (e ritorno) ) il valore di result ottenendo il seguente risultato. result to json

1
2
3

string json = JsonConvert.SerializeObject(result, Formatting.Indented);

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

[
  {
    "FilmName": "Film 1",
    "Category": "Category 1"
  },
  {
    "FilmName": "Film 2",
    "Category": "Category 1"
  },
  {
    "FilmName": "Film 3",
    "Category": "Category 2"
  },
  {
    "FilmName": "Film 4",
    "Category": "Category 3"
  }
]

Come avrete visto è davvero semplice e dal risultato estremamente potente applicandolo in casi reali.

Happy LINQ Inner JOIN a tutti!