How to Get a Nested Array Field - rethinkdb

I have a table user which contains an array Employee which stores the names ,i need to retrieve the last element of the array.
Could not figure out.Tried with count but count gives value =1 but there are three elements in an array
r.table("user")("Employee").count()
I need count actual elements in Employee array which is 3.Please help.Thanks

If you don't call .count on a specific document's field, it will return the count of the sequence of documents. In your request, 1 as a result actually means there is one document in the table.
You need to loop over each document to fetch the last name for each document, and you can fetch it directly using .nth:
r.table("user").map(function(document) {
return document.merge({lastName: document("Employee").nth(-1)});
// or, if you like complicated stuff
return {
id: document('id'),
lastName: document("Employee").nth(
document("Employee").count().sub(1)
)
};
});

Related

Laravel - How to extract one field for items in a model to an array?

I have a table that has a one-to-many relationship with another table.
Call them tables parent and child
child has a field called field1
I am trying to get an array of all field1 values.
In the parent Model, I have this function that gets me all children of parent
public function children()
{
return $this->hasMany(Child::class);
}
Now, in the parent model, I also want to get all field1 values.
I can get all the children like so:
$children = $this->children->all();
But I just can't figure out how to then index into this to get all the field1 values.
Maybe it is better to just use $this->children in which case it is a Collection and then use some sort of Collection method to extract field1?
I tried
$this->children->only(['field1'])
but that returned nothing, even though field1 certain exists.
Ideas?
Thanks!
You can use pluck method of collecion to get an array of field1 values:
$parent->child->pluck('field1');
Or better, you can use pluck method of query builder (see section Retrieving A List Of Column Values), being even more efficient, with same result:
$parent->child()->pluck('field1');
map() is the function you are looking.
$children = $this->children->all()->map(function($children) {
return $children->field1;
});
A shorter version using Higher Order Message.
$children = $this->children->all()->map->field1;

RethinkDB: Get only one record from cursor/selection

If I have a query that returns multiple results, how do I get a single element from the selection?
e.g.
r.db("test").table("things") // returns an array of things. I want one of them
Using limit(1) is not what I want because that returns an array.
Rethink DB supports getting the nth element so the query should be:
r.db("test").table("things").nth(0)
In the event that there are no elements, the above will fail with:
Index out of bounds: 0
The solution to this is to return a default object (null in my case) if no element exists.
r.db("test").table("things").nth(0).default(null)

How to compare array of elements with the linq query result in c#

How can i compare array of elements with the linq query. I will pass array of elements to the controller and i want to compare and display the records that only contains the passed elements.
I will pass an array of values like ["first","second","third"] and i want to compare these records with the linq query and generate the result that contains these three records
I'm not sure but you probably want something like this
String[] values = {"first", "second", "third"};
List<YourObject> query =
(from p in this.db.table
where values.Contains(p.values) select p).ToList();

Finding items from a list in an array stored in a DB field

I have a legacy database that has data elements stored as a comma delimited list in a single database field. (I didn't design that, I'm just stuck with it.)
I have a list of strings that I would like to match to any of the individual values in the "array" in the DB field and am not sure how to do this in Linq.
My list:
List<string> items= new List<string>();
items.Add("Item1");
items.Add("Item2");
The DB field "Products" would contain data something like:
"Item1,Item3,Item4"
"Item3,Item5,Item6"
"Item2,Item7,Item6"
"Item1,Item2"
"Item1"
My first pass at the Linq query was:
var results = (from o in Order
.Where(p=> items.Contains(p.Products)
But I know that won't work. because it will only return the records that contain only "Item1" or "Item2". So with the example data above it would return 0 records. I need to have it return two records.
Any suggestions?
There is a simple clever trick for searching comma-separated lists. First, add an extra , to the beginning and end of the target value (the product list), and the search value. Then search for that exact string. So for example, you would search ,Item1,Item3,Item4, for ,Item1,. The purpose of this is to prevent false positives, i.e., Item12,Item3 finding a match for Item1, while allowing items at the beginning/end of the list to be properly found.
Then, you can use the LINQ .Any method to check that any item in your list is a match to the product list, like the following:
var results = (from o in Order
.Where(o => items.Any(i => (","+o.Products+",").Contains(","+i+",")))
One way would be to parse the list in the Products field:
var results = (from o in Order
.Where(o => items.Any(i => o.Products.Split(',').Contains(i))
But that would parse the string multiple times for each record. You could try pulling back ALL of the records, parsing each record once, then doing the comparison:
var results = from o in Order
let prods = o.Products.Split(',')
where items.Any(i => prods.Contains(i))
select o;

Filter DataTable based on a Dictionary

I have a DataTable I would like to filter based on elements in a Dictionary.
The Dictionary key is an integer and the value portion is a class.
The contained class has a field called ItemId. This ItemId is a field in the datatable.
I have a value for the key portion of the dictionary.
What I would like is a LINQ query which returns an Enumerable subset of the datatable based on the value I have for the key.
In other words I want a result set of all records in the datatable whose ItemId column is in records contained in the dictionary for which I have the Key value.
Is this possible?
If I understand, you have a key. So with the key, you have one value of the dictionary. This seems very simple to obtain what you want :
int key = 3;
var enumerable = dataTable.Where(t => t.ItemId == dictionary[key].ItemId);

Resources