I have 2 tables as defined in the below image.
Now I want to query the "match table" with following checks;
UserA in FromUser
FromUser Rating is less than 0 or undefined.
Result should be:
Match Table Object
Must have count of meetings like UserA have 3 meetings with userB and 3 meetings with userC. so the result should be like following:
[{id:"",fromUser:"",toUser:"",MeetingCount:3},{id:"",fromUser:"",toUser:"",MeetingCount:3}]
How can I achieve this? What I tried so far:
I queried the match table and now I want to loop against each matchId from match detail table to get its data.
matQueryFrom.equalTo("FROM_USER_OBJECTID",user.id);
matQueryFrom.find().then(function(matchObjects){
if(matchObjects!=undefined && matchObjects.length>0){
var returnList = [];
for(var i=0;i<matchObjects.length;i++){
var vrmatchObj = matchObjects[i];
var matDetailQuery = new Parse.Query(Parse.Object.extend("VR_MATCH_DETAIL"));
matDetailQuery.equalTo("VRMatches", vrmatchObj.id);
matDetailQuery.find().then(function(detailArray){
returnList.push(detailArray);
}).then(function(){console.log(returnList);});
}
return returnList;
}
});
Can anyone guide me how can i achieve this? Currently the returnList is always empty.
Related
Suppose I have a user table with columns id, name, age
With a normal get query User::get(), I get all results with those column's value.
But instead of just getting the id, name, age columns, is it possible if I add another column, perhaps a column with an alias of future_age with a value of age + 1.
The above result could be achieved in SQL like so:
SELECT res.*, (SELECT sum(res.age+1) from users where id = res.id) as future_age from users as res
I've thought about looping through the result and creating new key. but I think this would make the query execution time slow when the data is lengthy.
Is it possible to create the future_age key/column (I don't know the term hehe) directly on the query?
Currently, this is my query:
$user = User::get();
$new_res = []
if($user->count() > 0) {
foreach ($user as $u) {
$u['future_age'] = $u->age + 1
$new_res[] = $u;
}
}
The query works tho, but I don't think this is good if I have a large set of data.
I have following two tables: Images and Articles
Both the tables are linked by the ImageId column.
An image can be associated with multiple articles.
For example: Images table has 100 rows and Articles table has 200 rows.
Out of these 100 images assume that only 90 are used across the Articles. In this case some of the images are repeated across many articles.
Here I want to fetch the unused 10 images (from Images table) and also want to include the ones that are associated with articles not more than 2 times. I want to ignore those images which are associated with the articles more than 2 times.
I tried the below linq query but it is not working for me.
var predicate = PredicateBuilder.True<Image>();
if (type != null && type != 0)
{
predicate = predicate.And(c => c.ImageType == type);
}
if (!string.IsNullOrWhiteSpace(keyword))
{
predicate = predicate.And(c => c.Name.Contains(keyword) || c.Keyword.Contains(keyword));
}
int skip = numberofImages * (page - 1);
var images = (from imgs in context.Images
join art in context.Articles on imgs.ImageId equals art.ImageId into unusedImages
from img in unusedImages.DefaultIfEmpty()
group img by imgs.ImageId into grouped
.AsExpandable()
.Where(predicate)
orderby Guid.NewGuid(), imgs.ImageId descending
select imgs)
.Skip(skip).Take(numberofImages).ToList();
Can any one help me to fix this issue?
split your query something like this,i wont complicate what is preferably simple
var usedArticles = (from item in context.Articles
group item by imageid into newList
select new
{
imageid = newList.key,
count =newlist.count
}).ToList();
var unwaantedImageIds = usedArticles.where(x=>x.count>2).Select(y=>y.imageId).ToArray();
var unwantedImages =context.image.where(x=>unwaantedImageIds.contains(x.imageId));
var result = context.images.Except(unwaantedImageIds).ToList();
I have three tables; user, car and user_x_car. user_x_car holds users who own car; user_id and car_id are stored. I want to get users who don't own a car as follows:
$car_owner = $this->db->select()->from('user_x_car')->get()->result();
for ($i = 0; $i < count($car_owners); $i++)
$car_owner_id[$i] = $car_owner[$i]->user_id;
$non_car_owner = $this->db->select()->from('user')->where_not_in('id', $car_owner_id)->get()->result();
I get what I want, however, is there any way to bypass the for loop in the middle which creates and array of id's selected in the first select. Is there any way to get array of selected user_ids directly?
you can do it by two queries like
first one get all ids from user_x_car table
$temp1=array();
$temp=$this->db->distinct()->select('user_id')->get('user_x_car')->result_array();
then from user table fetch those users who have no cars
foreach($temp as $each)
{
array_push($temp1,$each['user_id']);
}
$rs=$this->db->where_not_in('id',$temp1)->get('user');
if($rs->num_rows()>0)
{
$data=$rs->result_array();
print_r($data);die;
}
$data will print all users who have no car. Please let me know if you face any problem.
function get_unread_notifications_ids()
{
//get unread notifications ids
$this->db->select('GROUP_CONCAT(fknotification_id) as alll');
$this->db->from("te_notification_status_tbl");
$this->db->where('read_status',0);
$ids=$this->db->get()->row();
return $idss=str_replace(",","','",$ids->alll);
}
and second function like this:
function get_unviewed_photos_events(){
$idss = $this->get_unread_notifications_ids();
$this->db->select('img.*',False);
$this->db->from("te_notifications_tbl notif");
$this->db->join('te_images_tbl img','img.id=notif.reference_id','LEFT OUTER');
$this->db->where("notif.id IN('".$idss."')");
$rslt = $this->db->get()->result_array();
return $rslt;
}
Query
$non_car_owner = $this->db->query('SELECT user.*
FROM user LEFT JOIN user_x_car ON user_x_car.id=user.id
WHERE table2.id IS NULL')->result();
Here users who are not on the table user_x_car
foreach($non_car_owner as $user){
echo $user->user_id;
}
I'm trying to work with a LINQ result set of 4 tables retrieved with html agility pack. I'd like to process each one slightly differently by setting a variable for each (switch statement below), and then processing the rows within the table. The variable would ideally be the index for each of the tables in the set, 0 to 3, and would be used in the switch statement and to select the rows. I haven't been able to locate the index property, but I see it used in situations such as SelectChildNode.
My question is can I refer to items within a LINQ result set by index? My "ideal scenario" is the last commented out line. Thanks in advance.
var ratingsChgs = from table in htmlDoc.DocumentNode
.SelectNodes("//table[#class='calendar-table']")
.Cast<HtmlNode>()
select table;
String rtgChgType;
for (int ratingsChgTbl = 0; ratingsChgTbl < 4; ratingsChgTbl++)
{
switch (ratingsChgTbl)
{
case 0:
rtgChgType = "Upgrades";
break;
case 1:
rtgChgType = "Downgrades";
break;
case 2:
rtgChgType = "Coverage Initiated";
break;
case 3:
rtgChgType = "Coverage Reit/ Price Tgt Changed";
break;
//This is what I'd like to do.
var tblRowsByChgType = from row in ratingsChgs[ratingsChgTbl]
.SelectNodes("tr")
select row;
//Processing of returned rows.
}
}
ElementAt does what you're asking for. I don't recommend using it in your example, though, because each time you call it, your initial LINQ query will be executed. The easy fix is to have ratingsChgs be a List or Array.
You can also refactor out the switch statement. It is overkill when you only need to iterate through a list of items. Here is a possible solution:
var ratingsChgs = from table in htmlDoc.DocumentNode
.SelectNodes("//table[#class='calendar-table']")
.Cast<HtmlNode>()
select table;
var rtgChgTypeNames = new List
{
"Upgrades",
"Downgrades",
"Coverage Initiated",
"Coverage Reit/ Price Tgt Changed"
};
var changeTypes = ratingsChgs.Zip(rtgChgTypeNames, (changeType, name) => new
{
Name = name,
Rows = changeType.SelectNodes("tr")
});
foreach( var changeType in changeTypes)
{
var name = changeType.Name;
var rows = changeType.Rows;
//Processing of returned rows.
}
Also, why not store your rating change types in the HTML doc? It seems odd to have table information defined in the business logic.
So m.SourceCollection has 1000 records going into this, which is a collection of items with a Lat and Lon property; nothing else. I run this:
var results = from locs in m.PlacesBeen
group locs by new {locs.Lat, locs.Lon }
into myGroup
select new { Lat = myGroup.Key.Lat, Lon = myGroup.Key.Lon };
The next breakpoint, "results" has three items in it. I'm just trying to do a group by and get the unique amounts out, much like I would in SQL.
That query looks OK - what results were you expecting?
Btw, here's a simpler way to write the same query:
var results =
m.PlacesBeen.Select (loc => new {locs.Lat, locs.Lon }).Distinct();