How to get entity type in a T4 template - t4

In a T4 template I need to check if an entity has a custom attribute. I've tried doing the following but it returns null:
Type entityType = Type.GetType(entity.FullName);
Does anyone know how to go about doing this?

using the t4,
in the foreach use
entity.GetEntityType();

Related

Linq To Entities 'Only primitive types or enumeration types are supported' Error

I am using LinqPad to test my query. This query works when the LInqPad connection is to my database (LInq to SQL) but it does not work when I change the connection to use my Entity Framework 5 Model.dll. (Linq to Entity). This is in C#.
I have two tables called Plan and PlanDetails. Relationship is one Plan to many PlanDetails.
var q = from pd in PlanDetails
select new {
pd.PlanDetailID,
ThePlanName = (from p in this.Plans
where p.PlanID == pd.PlanID
select p.PlanName)
};
var results = q.ToList();
q.Dump(); //This is a linqpad method to output the result.
I get this error "NotSupportedException: Unable to create a constant value of type 'Domain.Data.Plan'. Only primitive types or enumeration types are supported in this context." Any ideas why this only works with Linq to SQL?
basically it means you are using some complex datatype inside the query for comparison.
in your case i suspect from p in this.Plans where p.PlanID == pd.PlanID is the culprit.
And it depends on DataProvider. It might work for Sql Data Provider, but not for SqlCE data Provider and so on.
what you should do is to convert your this.Plans collection into a primitive type collection containing only the Ids i.e.
var integers = PlanDetails.Plans.Select(s=>s.Id).ToList();
and then use this list inside.
var q = from pd in PlanDetails
select new {
pd.PlanDetailID,
ThePlanName = (from p in integers
where p == pd.PlanID
select pd.PlanName)
};
I got this error when i was trying to null check for a navigational property in the entity framework expression
I resolved it by not using the not null check in the expression and just using Any() function only.
protected Expression<Func<Entities.Employee, bool>> BriefShouldAppearInSearchResults(
IQueryable<Entities.Employee> briefs, string username)
{
var trimmedUsername = NameHelper.GetFormattedName(username);
Expression<Func<Entities.Employee, bool>> filterExpression = cse =>
cse.Employee.Cars.All(c =>
c.Employee.Cars!=null && <--Removing this line resolved my issue
c.Employee.Cars.Any(cur => cur.CarMake =="Benz")));
return filterExpression;
}
Hope this helps someone!
This is a Linqpad bug if you like (or a peculiarity). I found similar behaviour myself. Like me, you may find that your query works with an ObjectContext, but not a DbContext. (And it works in Visual Studio).
I think it has to do with Linqpad's inner structure. It adds MergeAs (AppendOnly) to collections and the context is a UserQuery, which probably contains some code that causes this bug.
This is confirmed by the fact that the code does work when you create a new context instance in the Linqpad code and run the query against this instance.
If the relationship already exists.
Why not simply say.
var q = from pd in PlanDetails
select new {
pd.PlanDetailID,
ThePlanName = pd.Plan.PlanName
};
Of course i'm assuming that every PlanDetail will belong to a Plan.
Update
To get better results from LinqPad you could tell it to use your own assembly (which contains your DbContext) instead of the default Datacontext it uses.

Getting dynamic property predicate by property name with queryDSL

I using Query DSL generated entity EntitySerializer in order to query JPA entities using QueryDSL (integrated with SpringData).
I’m receiving from the client property names and I want to create predicates for the properties that can be added (AND) to additional predicats.
I don’t know how to get from the EntitySerializer the predicate/Path that matches my property name. For example, let’s say we have a Person entity (with auto generated QPerson class) with a “name” property that I want to filter on (at the end I want to create a generic method). Here is the generic method:
Public Predicat getPredicatByPropertyName(String propertyName) {
QPerson p = QPerson.person;
person.getPredicat(“propertyName”).like(“tom”);
}
To create a String typed property just use the following snippet
new StringPath(p, propertyName)
which can then be used like this to create a Predicate instance
new StringPath(p, propertyName).like("tom")
I did it slightly different since as Timo said didn't work straightforward, here is it:
query.from(play);
query.where( Expressions.stringPath(play, "name").eq("New play") );
I know it could also be achieved by doing it separately:
StringPath column = Expressions.stringPath(play, "name");
query.from(play);
query.where( column.eq("New play") );
As of QueryDSL 5.0, I found two ways to do this independent of the column class:
First way: using just reflection:
Field pathField = p.getClass().getField(reportField.getFieldName());
ComparableExpressionBase<?> path = (ComparableExpressionBase<?>)
pathField.get(p);
Note: I use the ComparableExpressionBase class because it is extendend by all "path classes" that I found and it also can be used in select, orderBy and other functions to build the query
Second way: using reflection and ExpressionUtils:
Class<?> pParameterClass = (Class<?>) ((ParameterizedType) p.getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
Class<?> pathClass = pParameterClass.getDeclaredField(pathName).getDeclaringClass();
Path<?> path = ExpressionUtils.path(pathClass, p, pathName);
Note: using this way we first get the class of the entity of the table see this answer for an explanation on how to get a parametrized type. Next, we get the class of the path and finally we get the path with the ExpressionUtils.path method.

Dynamic Linq - no property or field exists in type 'datarow'

I am using Northwind Customers Table where I fill the dataset and get the datatable.
I am trying to use dynamic linq and want to select columnName dynamically
var qry = MyDataTable.AsEnumerable().AsQueryable().Select("new(Country)");
Right now I have hard coded country but even then I get this error
No property or field 'Country' exists in type 'datarow'
I would like to eventually change this query to take the column name dynamically.
Please help!!! thanks.
The important hint is here (in bold):
No property or field 'Country' exists
in type 'datarow'
The extension method AsEnumerable of the DataTable class returns an IEnumerable<T> where T has the type DataRow. Now the Select method of Dynamic LINQ wants to work with this type DataRow which hasn't a property Country of course.
You could try this instead:
var qry = MyDataTable.AsEnumerable().AsQueryable()
.Select("new(it[\"Country\"] as CountryAlias)");
it now represents a variable of type DataRow and you can use methods of this type and perhaps also the indexer in my example above. (Dynamic LINQ supports accessing array elements by an integer index, but I am not sure though if accessing an indexer with a string key will work.)
I've used Slauma's answer and it worked. In addition i was doing OrderBy with dynamic linq maybe this will help to someone. I'll just drop the code here.
string dynamicLinqText = $"it[\"{sortColumnName}\"] {sortDirection}"; //it["PERSON_NAME"] asc
result = result.AsEnumerable().OrderBy(dynamicLinqText).CopyToDataTable();

Entity Framework - Linq - Unable to create a constant value of type ‘System.Object’. Only primitive types

I have a method to build an expression for a linq query for a given type, property, and value. This works wonderfully as long as the property on the type is NOT nullable. Here is the example I am working from (http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees) I am calling the Equals method on the property. However I have discovered that the Equals method for Nullable types takes an Object as a parameter instead of the Nullable type. I attempted to use GetValueOrDefault to hide the null values but EF doesn't support that method. As a simple example the following code will throw an error:
decimal? testVal = new decimal?(2100);
var test = (from i in context.Leases where i.AnnualRental.Equals(testVal) select i).ToList();
However if you use == instead of the Equals() method it will work OK. I am not sure how to convert the code to use == instead of Equals() however. Any suggestions will be greatly appreciated.
If you say ==, you generate a BinaryExpression with NodeType as ExpressionType.Equal. http://msdn.microsoft.com/en-us/library/bb361179.aspx
If you say .Equals(x), you generate a MethodCallExpression. The MethodCallExpressions that LinqToEntities may translate into Sql is a limitted list (for example none of your own undecorated methods are in that list). Nullable<T>.Equals(x) is apparently not on that list.
Don't say .Equals(x) to LinqToEntities.

help! Linq query

I am getting error msg on the word Records - Type or namespace could not be found. Please help debugging it, what is missing?
if (ProjDDL1.SelectedItem.Value != "--") results = CustomSearch<Records>(results, s => s.Business == ProjDDL1.SelectedItem.Value);
Method CustomSearch:
private DataTable CustomSearch<TKEY>(DataTable dt, Func<Records, bool> selector)
{
DataTable results = (dt.AsEnumerable().Where(selector).CopyToDataTable());
return results;
}
Visual Studio will normally underline the item that's erroring when compiled. If you click on it and press Shift-Alt-F10, it will allow you to automatically add the namespace to the code. If you don't get a suggestion, this means that you haven't referenced the DLL that it needs.
Well, if it doesn't know what Records means, look at your references and using directives. Which namespace is the Records type in?
Why is your CustomSearch method generic anyway? It doesn't seem to use TKEY anywhere...

Resources