I have had some success getting the MSFT Dynamic Linq stuff to work, but now I need to create a "Where" clause that includes an Attribute.
The error I get is "No applicable aggregate method 'First' exists"
Here is my code:
where = "Element(XName.Get(\"procedure\")).Attributes(XName.Get(\"code\")).First() = \"28002\"";
var q2 = doc.Elements().Descendants("vocabularybody").AsQueryable().Where(where);
if (q2 != null && q2.Count() > 0)
foundItems.Add(item);
here is my XML
<vocabulary>
<vocabularyheader>
<vocabularyid>5</vocabularyid>
<vocabularyname>Scheduled Procedure</vocabularyname>
</vocabularyheader>
<vocabularybody>
<procedure code="28002" type="Surgery"/>
</vocabularybody>
</gazoontvocabulary>
I'm not familiar with the Dynamic LINQ library yet but shouldn't you need the equality operator (==) and not the assignment operator (=) for the where clause?
Related
var advocacy = (from c in svcContext.CreateQuery("lead")
join a in svcContext.CreateQuery("product")
on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
where a["capg_billingtimeframe"].Equals(126350000)
select new
{
dues = c["capg_calculatedduesbilling"],
product = c["capg_primaryproduct"],
listprice = a["price"],
eligibility = c.FormattedValues["capg_eligibility"]
});
That is my linq query and it is giving me the error: Invalid 'where' condition. An entity member is invoking an invalid property or method.
I have looked online everywhere and done their suggestions. I am not using Xrm.cs because late binding can be faster. I have tried using the == operand and I have tried doing (int) and Convert.ToInt32(a["capg_billingtimeframe"]) and even converting everything to a string. I will say that a["capg_billingtimeframe"] I know is an object (that's why I did those conversions.
I'm guessing by that integer that capg_billingtimeframe is an optionset. If it is, you need to cast it like this:
where ((OptionSetValue)a["capg_billingtimeframe"]).Value == 126350000
I used early bound and for getting the local I wrote:
OptionSetValue branch = this.InputTargetEntity.Attributes.Contains("capg_calculatorutilized") ? (OptionSetValue)this.InputTargetEntity["capg_calculatorutilized"] : (OptionSetValue)this.TargetPreImage["capg_calculatorutilized"];
I then had to get the Optionsets.cs using crmsvcutil and writing:
if (branch.Value == (int)capg_calculatorrequired.SectionA)
Works like a charm.
I have a linq query which I want to add some additional, optional WHERE conditions to using LinqKit Predicate Builder. However, I an struggling to get the additional predicate to work
This is my initial query:
var query = (from OP in ctx.OrganisationProducts
where OP.OrganisationID == orgID
orderby OP.Product.Name, OP.Product.VersionName
select OP).Include("Product");
As you can see, there is JOIN in there.
I then wish to add additional predicates if required:
if(!includeDisabledP42Admin || !includeDisabledOrgAdmin)
{
var pred = PredicateBuilder.True<OrganisationProduct>();
if (!includeDisabledP42Admin)
pred.And(op => op.Enabled);
if (!includeDisabledOrgAdmin)
pred.And(op => op.AccessLevel == "NA" || op.AccessLevel == "NU");
query = query.Where(pred);
}
However, the generated SQL is unchanged and the query returns the same number of rows.
I thought I might have had to do the Expand conversion as so:
query = query.AsExpandable().Where(pred);
But this causes a runtime error.
I think the issue is the fact that I am adding the predicate to a query that is already no longer a pure OrganisationProduct object, however I would like advise on how I insert my predicate at the right place.
Thanks and all :-)
You have to assign the return value of And to the predicate:
pred = pred.And(op => op.Enabled);
Side note: you may like this predicate builder that works without Expand/AsExpandable, but has the same syntax.
I have a PredicateBuilder which I got it from
http://www.albahari.com/nutshell/predicatebuilder.aspx
And here is how I use it:
var Expression = PredicateBuilder.True<UCM_UserAgent>();
Expression = Expression.And(item => item.AgentText == "TestUserAgent Value");
Func<UCM_UserAgent, bool> SearchCriteria = Expression.Compile();
var Data = Context.UCM_UserAgent
.Where(SearchCriteria)
.ToList();
And when I checked SQL Profiler on my database:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[AgentText] AS [AgentText],
FROM [dbo].[UCM_UserAgent] AS [Extent1]
Now, where is the condition I added? I could not solve this. Unfortunately, this Entity Framework and Predicate Builder - Predicates being Ignored in SQL Query did not help either.
Edit:
To eliminate any misunderstandings; I do not want to use as x => x.AgentText = "SomeText". Because I want to build a dynamic LINQ query, thus I use PredicateBuilder.
If you want the predicate to be hadled by LINQ to Entities you need to pass the Expression<Func<T,bool>> version to the Queryable.Where() method instead of passing the Func<T,bool> version to the Enumerable.Where() method as you are currently doing, i.e. do not compile the expression into IL before calling the Where method or you will necessarily get the LINQ to Objects version.
in my class i have a property with name "from" but i can't use it in my linq statement ?
is there any solution or i have to change my property name ?
lst1 = (from p in db.adv where p.isShowInMainPage && p.isShowInHeader && (p.from <= DateTime.Now) orderby p.id descending select p);
lst2= (from p in db.adv where p.isShowInMainPage && !p.isShowInHeader orderby p.id descending select p);
when i use p.from compile error occure :
Error 14 ; expected
in the SQL we could use [] ([from]) . is there any similar thing in LINQ
At (#from) in the beginning should work in C#, in VB.NET you use [from]
You could do as #Ales says and use an # symbol in front of the property name. Alternatively you could rename your property to From using Pascal casing - the .NET design guidelines on capitalisation conventions suggest using Pascal casing for properties.
I want to 'build' a combined query for Subsonic 3.0.0.3, what is the best way for this?
I tried;
Expression<Func<Person, bool>> exp = p => true;
Expression<Func<Person, bool>> fContinent = p => p.ContinentID == 1;
Expression<Func<Person, bool>> fType = p => p.TypeID == 1;
exp = Expression.Lambda<Func<Person, bool>>(Expression.AndAlso(exp, fContinent), exp.Parameters);
exp = Expression.Lambda<Func<Person, bool>>(Expression.AndAlso(exp, fType), exp.Parameters);
var personList = Person.Find(exp);
But that will give the exception "The binary operator AndAlso is not defined ..."
I also tried using predicates but that will throw exceptions as well (Expression.Invoke is not supported).
In subsonic 2 I would have used the SqlQuery object, but I would like to know the proper way to do this in version 3 using linq / expressions.
Have you tried And instead of AndAlso?
The right way to do this is to combine the lambda expression bodies, like this:
exp = Expression.Lambda<Func<Person, bool>>(
Expression.And(exp.Body, fContinent.Body), exp.Parameters);
Even if And is supported by your query provider, you'll also need to replace the parameter references in fContinent's Body with references to the parameter defined in exp--as is, your two expression bodies (combined with And) reference two distinct parameters, each named p.
See my answer to this question for the cleanest method to replace expression parameters.
I asked this question, but I am using the combined query in subsonic just like you.
In short, you want to use a PredicateBuilder to build up the query. When you want to execute it in your subsonic object (assuming ActiveRecord), use code like this:
var predicate = /* Build up predicate with PredicateBuilder */;
var recs = SubsonicClass.All().Where(predicate.Compile()).ToList();