Can you please explain me, what is VAR in LINQ, why we are using in all LINQ query.
var result = from sta in db.uploaddetails
where sta.Keyword==issue.uploaddetails.Keyword
select sta;
What is VAR here ??? why LINQ query is full different from SQL query.
var is an implicitly typed local variable, so the type is implied by the compiler based on the value it is assigned.
See http://msdn.microsoft.com/en-gb/library/bb384061.aspx for more info.
var is just an abbreviation for whatever type the right side returns.
It actually got nothing to do with LINQ, it it a standard C# keyword. You could write
var mylist = new List<int>;
as well
var result = from sta in db.uploaddetails
where sta.Keyword==issue.uploaddetails.Keyword
select sta;
Var is the keyword used under a scenario when an variable is declared with its datatype being unknown, or what the right hand side of the equation is going to return.
Related
I have a dynamic Linq Select statement of the form
var projection = result.AsQueryable().Select(string.Format("new({0},{1})",
model.XtabRow, model.XtabColumn));
This works fine and produces an IQueryable of Anonymous types.
However I an unable to convert it into IEnumerable to use linq on it as AsEnumerable method seems to be missing. I had to use reflection to extract field values in the end
There must be a better way - Any help would be great
Thanks
You can try something like this
var projection = result.AsQueryable().Select(string.Format("new({0},{1})",
model.XtabRow, model.XtabColumn));
var enumerableProjection = (from dynamic p in projection select p).AsEnumerable();
OR
var projection = result.AsQueryable().Select(string.Format("new({0},{1})",
model.XtabRow, model.XtabColumn));
var enumerableProjection = projection.Cast<dynamic>().AsEnumerable();
I'm using a LINQ to CRM from Advanced Developer Extension for MS CRM 4.0. It works fine with direct queries. But I've got a problem when query looks like this:
var connectionString = #"User ID=u; Password=p; Authentication Type=AD; Server=http://crm:5555/UO";
var connection = CrmConnection.Parse(connectionString);
var dataContext = new CrmDataContext(connection);
var data = from u in dataContext.Accounts
select new
{
Id = u.AccountID,
Name = u.AccountName,
};
var r = from n in data
where n.Name.StartsWith("test")
select new
{
Id = n.Id
};
r.Dump();
it throws an InvalidOperationException "Cannot determine the attribute name."
It's fine when a condition is directly in first query:
var data = from n in dataContext.Accounts
where n.AccountName.StartsWith("test")
select new
{
Id = n.AccountID,
Name = n.AccountName,
};
I cannot find any useful information about this kind of error. Is it a bug in Xrm Linq Provider?
Thanks in advance for any help.
Try eager loading the initial query with a ToList() so the latter query over your anonymous type is then evaluated locally. I get this is far from ideal if you have a lot of accounts but it'll prove the point. You essentially have a solution anyway in the last statement.
This is because the first query isn't executed at all until you call .Dump() at which point the entire expression including the second query is evaluated as one (deferred execution) by the provider which then looks for an attribute of Name.
I've been following with great interest the converstaion here:
Construct Query with Linq rather than SQL strings
with regards to constructing expression trees where even the table name is dynamic.
Toward that end, I've created a Extension method, addWhere, that looks like:
static public IQueryable<TResult> addWhere<TResult>(this IQueryable<TResult> query, string columnName, string value)
{
var providerType = query.Provider.GetType();
// Find the specific type parameter (the T in IQueryable<T>)
var iqueryableT = providerType.FindInterfaces((ty, obj) => ty.IsGenericType && ty.GetGenericTypeDefinition() == typeof(IQueryable<>), null).FirstOrDefault();
var tableType = iqueryableT.GetGenericArguments()[0];
var tableName = tableType.Name;
var tableParam = Expression.Parameter(tableType, tableName);
var columnExpression = Expression.Equal(
Expression.Property(tableParam, columnName),
Expression.Constant(value));
var predicate = Expression.Lambda(columnExpression, tableParam);
var function = (Func<TResult, Boolean>)predicate.Compile();
var whereRes = query.Where(function);
var newquery = whereRes.AsQueryable();
return newquery;
}
[thanks to Timwi for the basis of that code]
Which functionally, works.
I can call:
query = query.addWhere("CurUnitType", "ML 15521.1");
and it's functionally equivalent to :
query = query.Where(l => l.CurUnitType.Equals("ML 15521.1"));
ie, the rows returned are the same.
However, I started watching the sql log, and I noticed with the line:
query = query.Where(l => l.CurUnitType.Equals("ML 15521.1"));
The Query generated is:
SELECT (A bunch of columns)
FROM [dbo].[ObjCurLocView] AS [t0]
WHERE [t0].[CurUnitType] = #p0
whereas when I use the line
query = query.addWhere("CurUnitType", "ML 15521.1");
The query generated is :
SELECT (the same bunch of columns)
FROM [dbo].[ObjCurLocView] AS [t0]
So, the comparison is now happening on the client side, instead of being added to the sql.
Obviously, this isn't so hot.
To be honest, I mostly cut-and-pasted the addWhere code from Timwi's (slightly different) example, so some of it is over my head. I'm wondering if there's any adjustment I can make to this code, so the expression is converted into the SQL statement, instead of being determined client-side
Thanks for taking the time to read through this, I welcome any comments, solutions, links, etc, that could help me with this. And of course if I find the solution through other means, I'll post the answer here.
Cheers.
The big problem is that you're converting the expression tree into a delegate. Look at the signature of Queryable.Where - it's expressed in expression trees, not delegates. So you're actually calling Enumerable.Where instead. That's why you need to call AsQueryable afterwards - but that doesn't do enough magic here. It doesn't really put it back into "just expression trees internally" land, because you've still got the delegate in there. It's now wrapped in an expression tree, but you've lost the details of what's going on inside.
I suspect what you want is this:
var predicate = Expression.Lambda<Func<TResult, Boolean>>
(columnExpression, tableParam);
return query.Where(predicate);
I readily admit that I haven't read the rest of your code, so there may be other things going on... but that's the core bit. You want a strongly typed expression tree (hence the call to the generic form of Expression.Lambda) which you can then pass into Queryable.Where. Give it a shot :)
I have a simlar requirement where in i have 3 main entities in ADO.NET entity model... I am building a framework wherein basedon incoming XML root element i have to create an instance of specific entity using reflection and set its properties..
But when it comes to child entities.. I am not able to use LINQ queries on it as the type is not known at design time. PropertyInfo.GetValue gives me an object on which i can not run LINQ queries (even when i typecast it to IQueryable or IEnumerable). I am not even able to typecast it at design time as that would be kind of hardcoding and will fail my generic framework purpose.
I tried to use dynamic keyword.. but on that too i can not write LINQ queries.. it gives a message that LINQ queries are not supported on dynamic dispatch model.!!!
Can someone help..
Regards Badal
Like #Yuriy Faktorovich recommended, dynamic LINQ may be your best shot.
Another option you have is to dynamically build expression trees and execute your expression via reflection. Keep in mind that this is not easy and may take a little time to wrap your head around the expression API, but for simple expression it's not too bad.
Example: Say you want to replicate this expression:
p => p.FirstName == firstName
You can build the expression tree as follows:
var myType = Type.GetType("Person"); // <-- Find your type based on XML
var firstName = "John";
var param = Expression.Parameter(myType, "p");
var firstNameProperty = Expression.Property(param, "FirstName");
var constantExpression = Expression.Constant(firstName);
var equalsExpression = Expression.Equal(firstNameProperty, constantExpression);
var lambda = Expression.Lambda(equalsExpression, param);
The lambda will have a runtime type of Expression<Func<Person,bool>> which you can then pass to any IQueryable implementation.
UPDATE
Then to dynamcially call some method on your IQueryable you can do something similar to the following:
var queryableType = typeof(Queryable);
var whereMethod = queryableType.GetMethod("Where", BindingFlags.Public | BindingFlags.Static);
var parameters = new object[] { query, lambda }; // query is your IQueryable object
var list = whereMethod.Invoke(null, parameters);
I'm trying to wrap my head around linq -> nhib
I have a simple bit of sql that i'm trying to get working in nhibernate.linq
select * from
ColModel where ColModel.DataIndex
not in ('CostElement1', 'CostElement2', 'CostElement3')
and ColModel.ReportId = 1
The list of excluded DataIndex values comes in in the form of a List<string> called excludeNames
Here is what I have tried but it seems that it's not really feeling the love:
var s = base._sessionManager.OpenSession();
var query = from col in s.Linq<ColModel>()
where col.Report.Id == reportid &&
!(from c in s.Linq<ColModel>() select c.DataIndex).Contains(excludeNames)
select col;
return query.ToList();
the error:
The type arguments for method 'System.Linq.Enumerable.Contains<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I'm pretty sure I'm borfing this from the offset so any pointers would be well appreciated :)
w://
Contains doesn't accept a list.
There are ways to work around this in LINQ, but I'm not sure which, if any, of those will work in NH Linq
I think you have your exclusion backwards.
s = base._sessionManager.OpenSession();
var query = from col in s.Linq<ColModel>()
where col.Report.Id == reportid &&
!excludeNames.Contains(col.DataIndex)
select col;
return query.ToList();
Collection.Contains(item) will produce the SQL item in (...collection...), adding the negation will get you what you want.