How to do this in LINQ or EF.Core?

时间:2016-10-20 19:10:26

标签: c# linq entity-framework-core

Suppose I have a application with 3 models (class). All models have some type of relationship, like below:

User -> Group -> Database

A better explanation:

  • User: a class where store username, password, etc, and every group that the user belongs... So, one user can have one or more Group.
  • Group: story only the group name, and database that this group have access. A group can have one or more databases, and have one or more Users.
  • Database: store connection's property to a database.

Transforming this to class, maybe it's something like this:

class User {
  public int Id { get; set; }
  public string Username { get; set; }
  public string Password { get; set; }
  public List<Group> Groups { get; set; }
}

class Group {
  public int Id { get; set; }  
  public string Name { get; set; }
  public List<User> Users { get; set; } /* inverse */
  public List<Database> Databases { get; set; }
}

class Database {
  public int Id { get; set; }
  public string Hostname { get; set; }
  public string Username { get; set; }
  public string Password { get; set; }
  public string DbName { get; set; }
  public List<Group> Groups { get; set; } /* inverse */
}

So, now the real problem.

Suppose that I have only the Id of the user, nothing more, and I like to list all databases that the user have access, so, I must list all groups that the user have access, then in every group, I must list every databases that the group have access.

It's possible to make this in LINQ in a "direct" way? Or using EF.Core (again, "direct" way)?

Or it's better a more "long" way, like I describe above?

var user = _context.user.code_to_get_user_by_id(id);
var groups = _context.user.code_to_get_all_groups();
var databases = groups.code_to_get_all_databases();

foreach (var group in groups) {
  // do something
}

This is just a simple example. My real application have 4 class, with some different relationships and much more properties.

1 个答案:

答案 0 :(得分:1)

MSDN Enumerable.SelectMany Example

class PetOwner
{
    public string Name { get; set; }
    public List<String> Pets { get; set; }
}

public static void SelectManyEx1()
{
    PetOwner[] petOwners = 
        { new PetOwner { Name="Higa, Sidney", 
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi, Ronen", 
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price, Vernette", 
              Pets = new List<string>{ "Scratches", "Diesel" } } };

    // Query using SelectMany().
    IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets);

    Console.WriteLine("Using SelectMany():");

    // Only one foreach loop is required to iterate 
    // through the results since it is a
    // one-dimensional collection.
    foreach (string pet in query1)
    {
        Console.WriteLine(pet);
    }

    // This code shows how to use Select() 
    // instead of SelectMany().
    IEnumerable<List<String>> query2 =
        petOwners.Select(petOwner => petOwner.Pets);

    Console.WriteLine("\nUsing Select():");

    // Notice that two foreach loops are required to 
    // iterate through the results
    // because the query returns a collection of arrays.
    foreach (List<String> petList in query2)
    {
        foreach (string pet in petList)
        {
            Console.WriteLine(pet);
        }
        Console.WriteLine();
    }
}

/*
 This code produces the following output:

 Using SelectMany():
 Scruffy
 Sam
 Walker
 Sugar
 Scratches
 Diesel

 Using Select():
 Scruffy
 Sam

 Walker
 Sugar

 Scratches
 Diesel
*/

DotNetPearls SelectMany Example

using System;
using System.Linq;

class Program
{
  static void Main()
  {
    // Input.
    string[] array =
    {
    "dot",
    "net",
    "perls"
    };

    // Convert each string in the string array to a character array.
    // ... Then combine those character arrays into one.
    var result = array.SelectMany(element => element.ToCharArray());

    // Display letters.
    foreach (char letter in result)
    {
    Console.WriteLine(letter);
    }
  }
}

/* 
Output:

d
o 
t 
n 
e 
t 
p 
e 
r 
l 
s

*/

You can use LINQ SelectMany function. Check these links.

MSDN Select Many

Examples of DotNetPearls