Hello experts,
is there maybe a simple Linq equivalent to that?
Guid incidentid = (Guid)dataReader.GetValue(0); // just to let you know what this variable is foreach(Entity incident in crmResult.Entities) { if(incident.GetAttributeValue<EntityReference>("incidentid").Id == incidentid) { //Found! Do stuff here... } }
What I tried
if(crmResult.Entities.Where(x => x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid))
And
Entity incident = from x in crmResult.Entities where x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid select x;
And some other things but my knowledge in linq ends with FirstOrDefault.
*This post is locked for comments
Hi Radu,
sorry for the late response. Both of your solutions worked perfect.
Thanks,
Bernhard
good luck :)
I also get confused from time to time :)
Let me know if you need further help.
Regards,
Radu
Sorry, I got confused by all the articles I've read about LinQ. Your first solution already answered my question. I will try the code from your 2nd answer on monday out. (When I am back in office.)
Have a great weekend and thanks a lot,
Bernhard
so you basically want to find the first instance of an incident in a collection that matches a certain criteria ?
can you try:
var found =crmResult.Entities.Where(x => x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid).FirstOrDefault();
Also, if you want the IndefOf method you can convert the IEnumerable to List by calling ToList() method and and the List<T> has the IndefOff method - see docs.microsoft.com/.../system.collections.generic.list-1.indexof
Hope this helps,
Radu
Hi Radu,
thanks for your explanation and hint. I searched if it is possible to get out the index of the IEnumerable and found that:
https://stackoverflow.com/a/2471611/3772108
My idea was to do something like
Entity foundEntity = (Entity)crmResult.Entities[crmResult.Entities.Select((v, i) => new { value = v, index = i }).First(v => v.value.GetAttributeValue<Guid>("incidentid") == incidentid).index];
if(foundEntity != null) {//Code here..}
but the linq returns
System.InvalidOperationException: 'Sequence contains no matching element'
If I change the "First" to "Where" I have no access to ".index". When I change "First" to "FirstOrDefault" it throws the exception
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Maybe it is really better to stay with the much simpler foreach..
Thanks,
Bernhard
Hi Bernhard,
The Where method returns an IEnumerable<T>, please see docs.microsoft.com/.../system.linq.enumerable.where
You can't use this within an if statements, as the if statement expects a boolean to evaluate.
If you want to filter out records from a collection based on a filter criteria you can do following
var filteredIncidents = crmResult.Entities.Where(x => x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid)
Afterwards to apply logic against them, you need to iterate again through them and apply the logic
foreach(var incident in filteredIncidents)
{
//logic here
}
Hope this helps,
Radu
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,188 Super User 2024 Season 2
Martin Dráb 230,030 Most Valuable Professional
nmaenpaa 101,156