How to take the result of a Linq CompiledQuery - linq

Linq and compiled queries
This is my code:
I need to associated the value of a properties a.Value to a Literal.Text property.
Any idea how to do it?
Thanks for your valuable help!
using (var context = new CmsConnectionStringEntityDataModel())
{
context.CmsOptions.MergeOption = MergeOption.NoTracking;
var query = CompiledQuery.Compile<CmsConnectionStringEntityDataModel, IQueryable<CmsOption>>
(ctx => from a in ctx.CmsOptions where a.OptionId == 7 select a);
uxHeaderIncluder.Text = // What I do here?;
}

Have you looked at these examples:
http://www.blog.ingenuitynow.net/15+Minutes+On+LINQ+Compiled+Queries.aspx
http://msdn.microsoft.com/en-us/library/bb896297.aspx
The core of the approach is to spesify the input type(s) in the Compile method call.
Your compile call should be something like this:
CompiledQuery.Compile<CmsConnectionStringEntityDataModel,string,string, IQueryable<CmsOption>>
((ctx,str1,str2)=>from a in ctx.CmsOptions where a.OptionId == 7 && /* use str1 and str2 params here */ select a);

Related

How to declare a System.Linq.IQueryable variable with a complex query

I have a working query as below:
var result = from sch in schemeDashboard
join exp in Expenditure on sch.schemeId equals exp.SchemeCode
into SchExpGroup
where sch.SectorDepartmentId == selectedDepartmentId &&
sch.YearCode == StateManager.CurrentYear
orderby sch.ADPId
select new
{
ProjectName = sch.schemeName,
ADPNo = sch.ADPId,
Allocation = sch.CurrentAllocation,
Expenditures = from expend in SchExpGroup
where expend.YearCode == StateManager.CurrentYear &&
expend.DepartmentId == selectedDepartmentId &&
InvStatus.Contains(expend.Status)
orderby expend.ADPId
group expend by expend.InvoiceId
};
Now I need to declare "result" as: System.Linq.IQueryable<...> result = null; so that I can write the query in various If-else blocks with minor modification. (projectName is string, ADPNo is int, Allocation is decimal).
I have tried as follow:
System.Linq.IQueryable<string, int, decimal,
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, Expenditure>>> result = null;
But this is giving error as: The non-generic type 'IQueryable' cannot be used with typed arguments.
Can anyone point me in right direction? thanx.
The most generical class of C# is the class object. So you can declare your list with the Object type.
IQueryable <object> result = null;
Well I hope it works for you as it worked for me.

The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'

I have a Linq to Entities query of the following form:
var x = from a in SomeData
where ... some conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}
};
var y = from a in SomeData
where ... some other conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}
};
var results = x.Concat(y);
(This is a simplified example - the 'where' and 'select' clauses are more complex than shown here. I'm using separate queries statements as creating a single combined one is just too complicated, has too many conditionals and takes an age to compile)
Compiles fine, but fails at execution with the exception:
"The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'
Note, I am trying to project into a nested typed structure. If I call .ToList() on x and y prior to the Concat() it works fine. As a further point, one of my properties is an enum, but I am assigning to it using an integer wrapper property.
Is there a way I can do what I want to do without having to pull all the data into memory? Or is it the enum causing the failure?
Thanks,
T
Have you tried with
var results = x.Union(y);
?
Tiz
or
var x = (from a in SomeData
where ... some conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = (from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}).ToArray() //or DefaultIfEmpty
}).Concat(
from a in SomeData
where ... some other conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = (from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}).ToArray() //or DefaultIfEmpty
});
i had the similar problem while trying to concatenate or union multi sets of navigation properties into single IEnumerable, here is the code sample :
var requiredDocuments =
(from x in db.RequestTypes where (some condition) select x.RequiredDocuments)
.SelectMany(r => r).ToList<DataModel.RequiredDocument>()
.Concat(
(from c in db.Categories where (some condition) select c.RequiredDocuments)
.SelectMany(r => r).ToList<DataModel.RequiredDocument>()
)
.Concat(
(from f in db.Fields where (some condition) select f.RequiredDocuments)
.SelectMany(r => r).ToList<DataModel.RequiredDocument>()
);
If I understand correctly what you are trying to do, I have run into the same problem several times. The bottom line is, doing unions with nested projections is not supported and if you need to do that, you'll have to materialize the results with ToList first.

LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'

I have this linq query:
private void GetReceivedInvoiceTasks(User user, List<Task> tasks)
{
var areaIds = user.Areas.Select(x => x.AreaId).ToArray();
var taskList = from i in _db.Invoices
join a in _db.Areas on i.AreaId equals a.AreaId
where i.Status == InvoiceStatuses.Received && areaIds.Contains(a.AreaId)
select new Task {
LinkText = string.Format(Invoice {0} has been received from {1}, i.InvoiceNumber, i.Organisation.Name),
Link = Views.Edit
};
}
It has issues though. I'm trying to create tasks. For each new task when I set the link text to a constant string like "Hello" it is fine. However above I'm trying to build the property linktext using properties of the invoice.
I get this error:
base {System.SystemException} = {"LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression."}
Anyone know why? Anyone know an alternative way of doing this to make it work?
Entity Framework is trying to execute your projection on the SQL side, where there is no equivalent to string.Format. Use AsEnumerable() to force evaluation of that part with Linq to Objects.
Based on the previous answer I have given you I would restructure your query like this:
int statusReceived = (int)InvoiceStatuses.Received;
var areaIds = user.Areas.Select(x=> x.AreaId).ToArray();
var taskList = (from i in _db.Invoices
where i.Status == statusReceived && areaIds.Contains(i.AreaId)
select i)
.AsEnumerable()
.Select( x => new Task()
{
LinkText = string.Format("Invoice {0} has been received from {1}", x.InvoiceNumber, x.Organisation.Name),
Link = Views.Edit
});
Also I see you use related entities in the query (Organisation.Name) make sure you add the proper Include to your query, or specifically materialize those properties for later use, i.e.:
var taskList = (from i in _db.Invoices
where i.Status == statusReceived && areaIds.Contains(i.AreaId)
select new { i.InvoiceNumber, OrganisationName = i.Organisation.Name})
.AsEnumerable()
.Select( x => new Task()
{
LinkText = string.Format("Invoice {0} has been received from {1}", x.InvoiceNumber, x.OrganisationName),
Link = Views.Edit
});
IQueryable derives from IEnumerable, the main resemblance is that when you make your query it is posted to the database engine in it's language, the thin moment is where you tell C# to handle the data on the server(not client side) or to tell SQL to handle data.
So basically when you say IEnumerable.ToString(), C# gets the data collection and calls ToString() on the object.
But when you say IQueryable.ToString() C# tells SQL to call ToString() on the object but there is no such method in SQL.
The drawback is that when you handle data in C# the whole collection that you are looking through must be built up in memory before C# applies the filters.
Most efficient way to do it is to make the query as IQueryable with all the filters that you can apply.
And then build it up in memory and make the data formatting in C#.
IQueryable<Customer> dataQuery = Customers.Where(c => c.ID < 100 && c.ZIP == 12345 && c.Name == "John Doe");
var inMemCollection = dataQuery.AsEnumerable().Select(c => new
{
c.ID
c.Name,
c.ZIP,
c.DateRegisterred.ToString("dd,MMM,yyyy")
});
While SQL does not know what to do with a string.Format it can perform string concatenation.
If you run the following code then you should get the data you are after.
var taskList = from i in _db.Invoices
join a in _db.Areas on i.AreaId equals a.AreaId
where i.Status == InvoiceStatuses.Received && areaIds.Contains(a.AreaId)
select new Task {
LinkText = "Invoice " + i.InvoiceNumber + "has been received from " + i.Organisation.Name),
Link = Views.Edit
};
Once you actually perform the query this should be marginally faster than using AsEnumerable (at least that's what I found in my own code after having the same original error as you). If you are doing something more complex with C# then you will still need to use AsEnumerable though.

LINQ for LIKE queries of array elements

Let's say I have an array, and I want to do a LINQ query against a varchar that returns any records that have an element of the array anywhere in the varchar.
Something like this would be sweet.
string[] industries = { "airline", "railroad" }
var query = from c in contacts where c.industry.LikeAnyElement(industries) select c
Any ideas?
This is actually an example I use in my "Express Yourself" presentation, for something that is hard to do in regular LINQ; As far as I know, the easiest way to do this is by writing the predicate manually. I use the example below (note it would work equally for StartsWith etc):
using (var ctx = new NorthwindDataContext())
{
ctx.Log = Console.Out;
var data = ctx.Customers.WhereTrueForAny(
s => cust => cust.CompanyName.Contains(s),
"a", "de", "s").ToArray();
}
// ...
public static class QueryableExt
{
public static IQueryable<TSource> WhereTrueForAny<TSource, TValue>(
this IQueryable<TSource> source,
Func<TValue, Expression<Func<TSource, bool>>> selector,
params TValue[] values)
{
return source.Where(BuildTrueForAny(selector, values));
}
public static Expression<Func<TSource, bool>> BuildTrueForAny<TSource, TValue>(
Func<TValue, Expression<Func<TSource, bool>>> selector,
params TValue[] values)
{
if (selector == null) throw new ArgumentNullException("selector");
if (values == null) throw new ArgumentNullException("values");
if (values.Length == 0) return x => true;
if (values.Length == 1) return selector(values[0]);
var param = Expression.Parameter(typeof(TSource), "x");
Expression body = Expression.Invoke(selector(values[0]), param);
for (int i = 1; i < values.Length; i++)
{
body = Expression.OrElse(body,
Expression.Invoke(selector(values[i]), param));
}
return Expression.Lambda<Func<TSource, bool>>(body, param);
}
}
from c in contracts
where industries.Any(i => i == c.industry)
select c;
something like that. use the any method on the collection.
IEnumerable.Contains() translates to SQL IN as in:
WHERE 'american airlines' IN ('airline', 'railroad') -- FALSE
String.Contains() which translates to SQL LIKE %...% as in:
WHERE 'american airlines' LIKE '%airline%' -- TRUE
If you want the contacts where the contact's industry is LIKE (contains) any of the given industries, you want to combine both Any() and String.Contains() into something like this:
string[] industries = { "airline", "railroad" };
var query = from c in contacts
where industries.Any(i => c.Industry.Contains(i))
select c;
However, combining both Any() and String.Contains() like this is NOT supported in LINQ to SQL. If the set of given industries is small, you can try something like:
where c.Industry.Contains("airline") ||
c.Industry.Contains("railroad") || ...
Or (although normally not recommended) if the set of contacts is small enough, you could bring them all from the DB and apply the filter with LINQ to Objects by using contacts.AsEnumerable() or contacts.ToList() as the source of the query above:
var query = from c in contacts.AsEnumerable()
where industries.Any(i => c.Industry.Contains(i))
select c;
it will work if you build up the query as follows:
var query = from c in contacts.AsEnumerable()
select c;
query = query.Where(c=> (c.Industry.Contains("airline")) || (c.Industry.Contains("railroad")));
you just need to programmatically generate the string above if the parameters airline and railroad are user inputs. This was in fact a little more complicated than I was expecting. See article - http://www.albahari.com/nutshell/predicatebuilder.aspx
Unfortunately, LIKE is not supported in LINQ to SQL as per here:
http://msdn.microsoft.com/en-us/library/bb882677.aspx
To get around this, you will have to write a stored procedure which will accept the parameters you want to use in the like statement(s) and then call that from LINQ to SQL.
It should be noted that a few of the answers suggest using Contains. This won't work because it looks to see that the entire string matches the array element. What is being looked for is for the array element to be contained in the field itself, something like:
industry LIKE '%<element>%'
As Clark has mentioned in a comment, you could use a call to IndexOf on each element (which should translate to a SQL call):
string[] industries = { "airline", "railroad" }
var query =
from c in contacts
where
c.industry.IndexOf(industries[0]) != -1 ||
c.industry.IndexOf(industries[1]) != -1
If you know the length of the array and the number of elements, then you could hard-code this. If you don't, then you will have to create the Expression instance based on the array and the field you are looking at.

Linq to SQL: .FirstOrDefault() not applicable to select new { ... }

I just asked this question. Which lead me to a new question :)
Up until this point, I have used the following pattern of selecting stuff with Linq to SQL, with the purpose of being able to handle 0 "rows" returned by the query:
var person = (from p in [DataContextObject].Persons
where p.PersonsID == 1
select new p).FirstOrDefault();
if (person == null)
{
// handle 0 "rows" returned.
}
But I can't use FirstOrDefault() when I do:
var person = from p in [DataContextObject].Persons
where p.PersonsID == 1
select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode };
// Under the hood, this pattern generates a query which selects specific
// columns which will be faster than selecting all columns as the above
// snippet of code does. This results in a performance-boost on large tables.
How do I check for 0 "rows" returned by the query, using the second pattern?
UPDATE:
I think my build fails because I am trying to assign the result of the query to a variable (this._user) declared with the type of [DataContext].User.
this._user = (from u in [DataContextObject].Users
where u.UsersID == [Int32]
select new { u.UsersID }).FirstOrDefault();
Compilation error: Cannot implicitly convert type "AnonymousType#1" to "[DataContext].User".
Any thoughts on how I can get around this? Would I have to make my own object?
Why can you keep doing the samething? Is it giving you an error?
var person = (from p in [DataContextObject].Persons
where p.PersonsID == 1
select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode }).FirstOrDefault();
if (person == null) {
// handle 0 "rows" returned.
}
It is still a reference object just like you actual object, it is just anonymous so you don't know the actual type before the code is compiled.
Update:
I see now what you were actually asking! Sorry, my answer no longer applies. I thought you were not getting a null value when it was empty. The accepted response is correct, if you want to use the object out of scope, you need to create a new type and just use New MyType(...). I know DevEx's RefactorPro has a refactoring for this, and I think resharper does as well.
Call .FirstOrDefault(null) like this:
string[] names = { "jim", "jane", "joe", "john", "jeremy", "jebus" };
var person = (
from p in names where p.StartsWith("notpresent") select
new { Name=p, FirstLetter=p.Substring(0,1) }
)
.DefaultIfEmpty(null)
.FirstOrDefault();
MessageBox.Show(person==null?"person was null":person.Name + "/" + person.FirstLetter);
That does the trick for me.
Regarding your UPDATE: you have to either create your own type, change this._user to be int, or select the whole object, not only specific columns.
if (person.Any()) /* ... */;
OR
if (person.Count() == 0) /* ... */;
You can still use FirstOrDefault. Just have
var PersonFields = (...).FirstOrDefault()
PersonFields will be be null or an object with those properties you created.

Resources