Linq update list whilst looping through list [closed] - linq

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list which is accessible as follows:
List<PingtreeNode> _pingtree;
IEnumerable<PingtreeNode> ActiveNodes
{
get { return _pingtree.Where(x => x.Status == PingtreeNodeStatus.Ready);}
}
I also have a method, who's job it is to iterate over some of the ActiveNodes where a RuleClass exists. For each of these nodes, a validation method is called and if the node is found to be invalid, a new PingtreeNodeStatus is set.
void ValidateBuyerRules()
{
var nodes = ActiveNodes.Where(x => x.RuleClass.IsNotNullOrEmpty());
foreach (var n in nodes)
{
var isValid = NewRuleSet(n.RuleClass).Validate(_appForm);
if (!isValid) n.Status = PingtreeNodeStatus.FailedCritera;
}
}
One of the properties of PingtreeNode is 'Product', and a product can exists several times within the list ActiveNodes.
What I want to do is this. If isValid is true for a product, I need to set all subsequent nodes containing the same Product to a PingtreeNodeStatus.FailedCriteria. In other words ignore them further down the nodes list and don't perform validation on them.
I know how to iterate through the loop and do this but am concerned about updating a list whilst already being inside the list. Is there a safe way of achieving this?

It's not possible to update a list while iterating over it, however that's not what you're doing.
You're not modifying the list but properties of the items in the list, this is perfectly safe.

Related

How to access the Id's of both tables which is identical in Laravel view [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have used join that joins to two tables that has the same id column name, while passing it to the view and how to access the ids of both tables.
You can use SELECT as:
$example = Example::join('example2', 'example2.id, '=', 'example.id')
->select([
'example.id AS id',
'example2.id AS id2'
])
->where('')
->get();
view:
foreach($example as $result)
{
$id = $result->id;
$id2 = $result->id2;
}

Array to string conversion when run eloquent command [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Controller :
public function index(){
$data = Personal::all();
return view('frontend/dashboard');
}
Error :
Array to string conversion
And also show :
\vendor\laravel\framework\src\Illuminate\Database\Grammar.php:39
change
return view('frontend/dashboard');
to
return view('frontend.dashboard');
please make sure your file name is dashboard.blade.php

Laravel - How to use isDirty() [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to use Laravel isDirty() which helps me to get only modified values,
But there is a problem when I'm trying to use, it always return false
public function update(Request $request, $id) {
$client = Client::find($id);
dd($client->isDirty('name'));
}
It always return false.
It returns false because you haven't done anything to $client.
Thus, it is not "dirty" - it is "clean".
If you do something like $client->name = str_random(40); it'll become dirty.
To get only modified attributes, you need to use the getDirty() method. isDirty() only shows if there are any modified attributes:
$client = Client::find($id);
$client->name = 'Some New Name';
$modifiedAttributes = $client->getDirty();
If you want to check if any of properties were modified in the submitted form or not, you can do it like this:
if ($client->name === $request->name)

Unable to properly create nested hash after reading data from Excel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
There is one quiz excel sheet which has 3000 questions.which has question_no, options, test_name, category, question_text,correct_answer,tags fields. using "Spreadsheet" gem i have parsed data from excel.
Now I have one hash which stores "tags type" as key and tag related questions are stored in an array as value.
if questions_hash[tags_string] == nil
questions_hash[tags_string] = []
questions_hash[tags_string].push question
which look like this,
tags = {"tag1" => [value],"tag2" => [value] }
and other hash is,
categories = {"cat1" => "", "cat2" => "" }
and i want to assign tags hash as a value in categories hash.like
categories = {"cat1" => {"tag1" => [value], "tag2" => [value]} ,"cat2" => {"tag3" =>[value] }}
how can i do this?
Hash does has push method.
To insert value into Hash use simple assignment as:
questions_hash[tags_string] = question
Similarly, if you want to insert questions_hash as value to catetory hash use:
hash['History'] = questions_hash
That's it.

How to parse json string in ajax? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have response like this how to parse this data ? any suggestions
{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21
03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21
03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21
03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for
you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21
01:09:58","username":"users"},{"job_id":"24","message":"test job for
you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21
01:09:58","username":"amit"},{"job_id":"25","message":"test job for
you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21
01:09:58","username":"test"}]}
Thanks in advance
You can do this:
JSON.parse('{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"users"},{"job_id":"24","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21 01:09:58","username":"amit"},{"job_id":"25","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"test"}]}')
EDIT:
to get all job_id do this:
var obj = JSON.parse('{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"users"},{"job_id":"24","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21 01:09:58","username":"amit"},{"job_id":"25","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"test"}]}')
var job_id_array = new Array();
for(var i = 0;i< obj.response.length ; i++){
job_id_array.push(obj.response[i].job_id);
}
You can use JQuery.parseJSON()
More details : http://api.jquery.com/jQuery.parseJSON/
If your JSON string would be stored in the json variable, you may do the follwoing (given that the environment supports ECMAScript5):
var o = JSON.parse(json);
var result=[];
o.response.forEach(function(e){
result.push(e.job_id);
});
console.log(result);
which outputs the content of the result array:
["29", "28", "27", "26", "24", "25"]
Here's a live demo and a good answer which addresses your question.

Resources