I have a Json response with tree structure array. Now the problem is i have to display that array in table view. The array format is like
details = {
id = 002;
name = Rohit;
fid = 123;
"Friends" = (
{
id = 003;
name = "Sham";
fid = 2355;
"Friends" = (
{
id = 252;
name = Ram;
Fid = 8568;
"Friends" = (
{
id = 7545;
name = "Rahul";
Fid = 874;
"Friends" = (
);
},
{
id = 77554;
name = "LCMff";
pid = 45425;
"Friends"= (
);
},
{
id = 4545;
name = peter;
fid = 4548;
"Friends"= (
{
id = 785612;
name = "john";
fid = 45;
"Friends" = (
);
}
);
},
},
Above is the example. Now i have to display Friends list in table view on click of name.Also i have to display the name in header like bread crumb. Help me out to resolve this.
Related
I want to figure out how I can reference an item and it's attributes created in a Terraform for-each loop.
The current situation is with AWS IAM roles and policy attachments.
aws_iam_role module:
main.tf
resource "aws_iam_role" "iam_role" {
for_each = var.roles
name = each.value.role_name
description = each.value.description
assume_role_policy = file(each.value.assume_role_policy)
max_session_duration = each.value.max_session_duration
}
variables.tf:
variable "roles" {
type = map(object({
role_name = string
description = string
assume_role_policy = string
max_session_duration = string
}))
}
outputs.tf:
output "id" {
value = [
for role in aws_iam_role.iam_role:
role.id
]
}
output "arn" {
value = [
for role in aws_iam_role.iam_role:
role.arn
]
}
output "name" {
value = [
for role in aws_iam_role.iam_role:
role.name
]
}
output "RoleNameARNMapping" {
value = {
for role in aws_iam_role.iam_role:
role.name => role.arn
}
}
aws_policy_attachment module:
main.tf
resource "aws_iam_policy_attachment" "policy_attachment" {
for_each = var.attachments
name = each.value.name
users = each.value.users
roles = each.value.roles
groups = each.value.groups
policy_arn = each.value.policy_arn
}
variables.tf
variable "attachments" {
type = map(object({
name = string
users = list(string)
roles = list(string)
groups = list(string)
policy_arn = string
}))
}
Role Configuration:
module "IAM_Roles" {
source = "../modules/aws_iam_role"
roles = {
"iam_role_for_lambda" = {
role_name = "iam_role_for_lambda"
description = "IAM Role For Lambda"
assume_role_policy = "../dev/roles/IAMRoleForLambda.json"
max_session_duration = 3600
}
}
}
I would need to reference the name of the created roles in the policy attachments configuration like this:
module "lambda_role_attachments" {
source = "../modules/aws_iam_policy_attachment"
attachments = {
"lambda-cloudwatch-access" = {
name = "lambda-cloudwatch-access"
users = null
roles = [module.IAM_Roles['iam_role_for_lambda'].name]
groups = null
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
}
}
}
This, unfortunately, does not work. Terraform does not like the quotes:
Single quotes are not valid. Use double quotes (") to enclose strings.
Expected the start of an expression, but found an invalid expression token.
The error you getting is clear Single quotes (') are not valid. Use double quotes (")
But you if you want to access the name by ID ["iam_role_for_lambda"] you need to change the name output to a map:
output "name" {
value = {
for role in aws_iam_role.iam_role :
role.name => role.name
}
}
then where you consume that output it would be:
module "lambda_role_attachments" {
source = "../modules/aws_iam_policy_attachment"
attachments = {
"lambda-cloudwatch-access" = {
name = "lambda-cloudwatch-access"
users = null
roles = [module.IAM_Roles.name["iam_role_for_lambda"]]
groups = null
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
}
}
}
I have a fully working prototype here:
https://github.com/heldersepu/hs-scripts/tree/master/TerraForm/modules
But if you already know the name maybe it would be simpler to just do:
module "lambda_role_attachments" {
source = "../modules/aws_iam_policy_attachment"
attachments = {
"lambda-cloudwatch-access" = {
name = "lambda-cloudwatch-access"
users = null
roles = ["iam_role_for_lambda"]
groups = null
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
}
}
}
Let's suppose we have a Linq query like this:
int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId);
var stock = from i in _stockService.GetStock()
join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id
where ur.ComapnyId == companyID
select new StockVM
{
Product = ur.ItemName,
Quantity = i.Quantity,
BatchID = i.BatchID,
StockStatus = i.StockStatus,
MfgDate = i.MfgDate,
ExpDate = i.ExpDate,
};
Result
How to do a "Group By Product" with sum of Quantity in this linq query?
I need to only get max ExpDate firstOrDefault
try something like this:
int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId);
var stock = from i in _stockService.GetStock()
join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id
where ur.ComapnyId == companyID
group new { Quantity = i.Quantity } by new { ur.ItemName } into g
select new { Product = g.Key, TotalQuantity = g.Sum() } ).ToList() ;
List<StockVM> _lst = new List<StockVM>();
foreach(var item in stock ){
StockVM row = new StockVM();
row.Product = item.ItemName;
//....
_lst.Add(row);
}
I have a method that gets some data from the database by some linq queries. The data is shown as expected but not in the order I wan't. I need to sort the products I get from the database by the TotalQuantity property shown in query 1 and 2. Im trying to use OrdeBy but I'm not sure how to add it in this context. Need some help with this one.
This is my method:
public IList<BestsellersReportLine> DailyBestsellersReport()
{
OrderStatus os;
PaymentStatus ps;
ShippingStatus ss;
int billingCountryId = 0;
int recordsToReturn = 10;
int orderBy = 1;
int groupBy = 1;
var range = new
{
startTimeUtc = DateTime.Today.AddDays(-1),
endTimeUtc = DateTime.Today.AddSeconds(-1),
CreatedOnUtc = DateTime.Today.AddDays(-1),
};
var query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (o.CreatedOnUtc >= range.startTimeUtc && o.CreatedOnUtc <= range.endTimeUtc)
select opv;
var query2 = groupBy == 1 ?
//group by product variants
from opv in query1
group opv by opv.ProductVariantId into g
select new
{
EntityId = g.Key,
TotalAmount = g.Sum(x => x.PriceExclTax),
TotalQuantity = g.Sum(x => x.Quantity),
}
:
//group by products
from opv in query1
group opv by opv.ProductVariant.ProductId into g
select new
{
EntityId = g.Key,
TotalAmount = g.Sum(x => x.PriceExclTax),
TotalQuantity = g.Sum(x => x.Quantity),
};
switch (orderBy)
{
case 1:
{
query2 = query2.OrderByDescending(x => x.TotalQuantity);
}
break;
case 2:
{
query2 = query2.OrderByDescending(x => x.TotalAmount);
}
break;
default:
throw new ArgumentException("Wrong orderBy parameter", "orderBy");
}
if (recordsToReturn != 0 && recordsToReturn != int.MaxValue)
query2 = query2.Take(recordsToReturn);
var result = query2.ToList().Select(x =>
{
var reportLine = new BestsellersReportLine()
{
EntityId = x.EntityId,
TotalAmount = x.TotalAmount,
TotalQuantity = x.TotalQuantity
};
return reportLine;
}).ToList();
return result;
}
What about return
result.OrderBy(x => x.totalQuantity).ToList();
Update:
I can only think of adding .ToList() to the end again.
Remove the first ToList() after query2 as mentioned below:
var result = query2.Select(x =>
{
var reportLine = new BestsellersReportLine()
{
EntityId = x.EntityId,
TotalAmount = x.TotalAmount,
TotalQuantity = x.TotalQuantity
};
return reportLine;
}).ToList();
Im having this weird problem with creating Json dynamically.... for some reason this doesnt work
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
Its giving me a weird "Could not translate expression....... into sql" exception
but with this minor change it works just fine
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
Notice that the change is just to make id=5 instead of dynamic.
also, this works correctly but I dont like it.
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
I'm not sure if this will solve your problem, but assuming companies is an IQueryable from the DataContext, try calling ToList() on it so that the select expression doesn't get sent to the database.
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies.ToList()
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
It's trying to translate the i to SQL and failing. The query that gets generated is something close to
SELECT companies.Id,
'' + companies.Name + ''
FROM companies
Basically, your string concatenation is being sent to your SQL server.
I have the following code which adapts linq entities to my Domain objects:
return from g in DBContext.Gigs
select new DO.Gig
{
ID = g.ID,
Name = g.Name,
Description = g.Description,
StartDate = g.Date,
EndDate = g.EndDate,
IsDeleted = g.IsDeleted,
Created = g.Created,
TicketPrice = g.TicketPrice
};
This works very nicely.
However I now want to populate a domain object Venue object and add it to the gig in the same statement. Heres my attempt....
return from g in DBContext.Gigs
join venue in DBContext.Venues on g.VenueID equals venue.ID
select new DO.Gig
{
ID = g.ID,
Name = g.Name,
Description = g.Description,
StartDate = g.Date,
EndDate = g.EndDate,
IsDeleted = g.IsDeleted,
Created = g.Created,
TicketPrice = g.TicketPrice,
Venue = from v in DBContext.Venues
where v.ID == g.VenueID
select new DO.Venue
{
ID = v.ID,
Name = v.Name,
Address = v.Address,
Telephone = v.Telephone,
URL = v.Website
}
};
However this doesnt compile!!!
Is it possible to adapt children objects using the "select new" approach?
What am I doing so very very wrong?
Your inner LINQ query returns several objects, not just one. You want to wrap it with a call like:
Venue = (from v in DBContext.Venues
where v.ID == g.VenueID
select new DO.Venue
{
ID = v.ID,
Name = v.Name,
Address = v.Address,
Telephone = v.Telephone,
URL = v.Website
}).SingleOrDefault()
Your choice of Single() vs. SingleOrDefault() vs. First() vs. FirstOrDefault() depends on what kind of query it is, but I'm guessing you want one of the first two. (The "OrDefault" variants return null if the query has no data; the others throw.)
I also agree with Mike that a join might be more in line with what you wanted, if there's a singular relationship involved.
Why are you doing a join and a sub select? You can just use the results of your join in the creation of a new Venue. Be aware that if there is not a one to one relationship between gigs and venues you could run into trouble.
Try this:
return from g in DBContext.Gigs
join venue in DBContext.Venues on g.VenueID equals venue.ID
select new DO.Gig { ID = g.ID, Name = g.Name, Description = g.Description,
StartDate = g.Date, EndDate = g.EndDate, IsDeleted = g.IsDeleted,
Created = g.Created, TicketPrice = g.TicketPrice,
Venue = new DO.Venue { ID = venue.ID, Name = venue.Name,
Address = venue.Address, Telephone = v.Telephone,
URL = v.Website }