-2

I have a general understanding of the Lambda operator =>, but I simply don't understand where the properties of variables z, c, s come from (as in z.Name). There's some object type inference magic happening that I don't understand.

public class DbReadService : IDbReadService
{
    private VODContext _db;
    public DbReadService(VODContext db)
    {
        _db = db;
    }


    private (IEnumerable<string> collections, IEnumerable<string> references) GetEntityNames<TEntity>() where TEntity : class
    {
        var dbsets = typeof(VODContext).GetProperties(BindingFlags.Public | BindingFlags.Instance)
            .Where(z => z.PropertyType.Name.Contains("DbSet"))
            .Select(z => z.Name);

        var properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        var collections = properties.Where(l => dbsets.Contains(l.Name)).Select(s => s.Name);

        var classes = properties.Where(c => dbsets.Contains(c.Name + "s")).Select(s => s.Name);

        return (collections: collections, references: classes);




    }
}
brad
  • 605
  • 2
  • 7
  • 18
  • `c` is just an arbitrary name for a placeholder for all the items that get enumerated from `properties`, ie `c` is (each) one of those `properties`. In the 2nd clause (`Select`) the very same item is now just as arbitrarily names `s`.. A quite confusing choice of names. Always chosing `x` or `p` would be better imo.. – TaW Jul 07 '19 at 16:44
  • This documentation on [Lambda expressions](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) will help. There is some magic of [type inference](https://www.developer.com/net/csharp/article.php/3601646/Go-Inside-C-30s-Type-Inference-Process.htm) going on. – Mat J Jul 07 '19 at 16:46
  • Yes, Mat, type inference is really my question. I know the c parameter can be anything, but somehow c is inferred to be some mysterious object type to make the .Name property available. I'm going to restate this question with additional code. – brad Jul 07 '19 at 16:59
  • 1
    _I know the c parameter can be anything_ __No__ it can't __be__ _anything_ !! It can be __named__ anything but it will always __be__ one element of the `properties` list after another. The actual type is inferred just as in a `foreach (var x in properties)..` – TaW Jul 07 '19 at 17:12
  • TaW, "properties" is a System.Reflection.PropertyInfo type. There is no Name property. – brad Jul 07 '19 at 17:23
  • 2
    TaW, after further inspection, the base type of PropertyInfo is MethodInfo, which does have a Name property. I think the mystery is solved. – brad Jul 07 '19 at 17:33

1 Answers1

0

LINQ functions operate on generic IEnumerable. when you call any LINQ function on IEnumerable e.g. List.where(c => c.variable), c would be an instance of someType.
In your example, I would assume "properties" is a list or array of Property and thus, c would be an instance of Property.

Sohaib Jundi
  • 1,576
  • 2
  • 7
  • 15