views:

389

answers:

2

I have Enitity Type, Name of Primary Key and Guid of Primary Id. I want to get element of such Id in LinqToSql.

model.GetTable<T>().Where(t => here equality  );

I think I need to generate that Expression myself, but I dont know how :(

A: 

I look forward, and after searching through generated by compiler code, in Reflector I found this creation of lambda.

    public static T GetById(Guid id)
    {
        Type entType = typeof(T);

        if (!CheckTable(entType)) {
            throw new TypeLoadException(string.Format(
                "{0} is not Table Entity, has no attribute Table", entType.FullName));
        }

        string property = GetPrimaryKeyName(entType).Name;

        ParameterExpression cs;
        var lambda = Expression.Lambda<Func<Personal, bool>>(
                Expression.Equal(
                        Expression.Property(
                                cs = Expression.Parameter(typeof(T), "p"), 
                                entType.GetProperty(property).GetGetMethod()
                        ), 
                        Expression.Constant(id), 
                        false, 
                        typeof(Guid).GetMethod("Equals")
                ), new ParameterExpression[] { cs }
        );

        return Connection.Model.GetTable<T>().Single(lambda);
    }

The thing what I need, but I have compiler exception:

Error 5 The type arguments for method 'System.Linq.Enumerable.Single(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. D:\Projects\Own\Yabeda\Source\trunk\med\Yabeda.Med.Mvc\Data\Oper.cs 48 20 Yabeda.Med.Mvc

Can't find what this Error means!

dynback.com