I have two list of data stored in
1. final profiles = context.watch<ProfilesBloc>().state.profiles;
2. final users= context.watch<UsersBloc>().state.users;
I want to build the list with the only whose profile.id matches the user.id
I tried by if (user.id == profiles.id) but it's not working
any help?
var newUser = users.where((user) => user.id.toLowerCase().contains(profiles.id.toLowerCase()).toList();
you can basically use this method to check a condition to create a new list. Feel free to alter the codes as per your requirements.
Related
I create an expense collection in firebase but when I access it in other screen using provider every time I add new item It duplicate the previous item as I continue inserting items it duplicate them?
final summaryDataExpense = Provider.of<ExpensesData>(context).expenseList;
final filterByYearExpense = summaryDataExpense
.where((element) =>
DateTime.parse(element['itemDate']).year == currentYear)
.toList();
var totalExpenses = filterByYearExpense.map((e) => e['itemPrice']).toList();
totalExpenses return wrong length of the items existed
You need to replace type of expenseList from List to Set.
Set will take care of the duplicates.
I have an Articles table and it has a relation with the Authors table. Each author has a name property. I want to make a GET request for the articles and receive the articles sorted based on their author's name.
When I use _sort=author in the url parameters, I get the articles sorted based on the author object's ID.
When I use _sort=author.name in the url parameters, I get the articles in a seemingly random order but definitely not based on author.name.
How can I get the data sorted based on author.name?
I use mongoDB as my database.
That is the default behavior of _sort. However, you can simply accomplish this by overriding the find method in api/article/services/article.js to sort by Author's name like so:
module.exports = {
async find(params, populate) {
let result = await strapi.query('article').find(params, populate);
if (params._sort == 'author')
return result.sort((a, b) => (a.author.name > b.author.name) ? 1 : -1);
return result;
},
}
Check the documentation for customizing services to get more info: https://strapi.io/documentation/v3.x/concepts/services.html#concept
I am using Entity Framework 7 Code First
I have a function that needs to returns a list of Countries(ids,Names) linked to a User.
The User isn't directly linked to the Country but is linked via the City. City is linked to State. State is linked to Country.
I decided to use a GroupBy to get the list of countries.
public async Task<IEnumerable<Country>> Search(int userId)
{
var table = await _db.Cities
.Include(ci => ci.States.Country)
.Select(ci => ci.States.Country)
.OrderBy(co => co.CountryName)
.GroupBy(co=>co.pk_CountryId)
.ToListAsync()
;
return table;
}
However I get the error:
CS0266 Cannot implicitly convert type
'System.Collections.Generic.List <System.Linq.IGrouping> to
'System.Collections.Generic.List'
How do I return a variable IEnumerable<Country> as that is what the receiving code expects i.e. a list of Countries?
Am I doing my grouping correct?
For performance I assume grouping is better than a distinct or a contains
If you want to have the distinct countries, you can use a select afterwards to select the first country in each IGrouping<int,Country>:
public async Task<IEnumerable<Country>> Search(int userId)
{
return await _db.Cities
.Include(ci => ci.States.Country)
.Select(ci => ci.States.Country)
.OrderBy(co => co.CountryName)
.GroupBy(co=>co.pk_CountryId)
.Select(co => co.FirstOrDefault())
.ToListAsync();
}
Also a little sidenote, the Include isn't necessary here, eager loading the countries would only be useful if you were to return the States and wanted its Country property be populated. The Select makes sure you're grabbing the Country, you're not even fetching the states anymore from database.
I have two model classes first one is "Ticket" and other is "ExpTicket" there is only one additional column/properties in ExpTicket and others properties are same in both lists. I extract the data from database in the form of ExpTicket list and then re-assign all column/properties to Ticket List except one additional column which does not exist in Ticket list.
But iam unable to assign data from "ExpTicket" list to "Ticket" list. If anyone can timely help I shall be very thankful. Following is the code which i need to convert From ExpTicket into "Ticket" List but failed. Please help.
var GetTicket = ticketRepository.ExpTicket(r => r.TicketId == TicketId).ToList();
List<Ticket> CTicket = GetTicket.ToList();
First you have:
var GetTicket = ticketRepository.ExpTicket(r => r.TicketId == TicketId).ToList();
Then make a query:
var CTickets = (from t in GetTicket
select new Ticket{field1=t.field1,field2=t.field2...}).ToList();
Or just readjust your model to use TPT (Table per type, Table per type code first) Good luck
var CTickets = new List<Ticket>();
var ExpTicket = ticketRepository.Where(r => r.TicketId == TicketId);
foreach (var ticket in ExpTicket)
{
CTickets.Add(new Ticket { TicketId = ticket.TicketId, ... and so on });
}
// CTickets is the new list of tickets
Using JPA, I have a list of entries from my database :
User(id, firstname, lastname, email)
That I get by doing:
List<User> users = User.find("lastname = ?", "smith");
And I'd like to update all in one request, by doing something like this :
"UPDATE USER SET email = null IN :list"
and then set the parameter "list" to users
Is it possible? if so, how?
Thanks for your help :)
Well, you could embed the query that you used to obtain list in the where clause of the update.
UPDATE User a SET a.email = null
WHERE user IN (SELECT b FROM User b WHERE lastName = :?)
By doing this you'd be doing the query to search the list and the update in single update query.
How do you like that? Do you think this could work?
-EDIT-
Since you want to use the original list of items instead of a list just retrieved from the database, you can still ensure you build the original list like this
UPDATE User a SET a.email = null
WHERE user IN (SELECT b FROM User b WHERE lastName IN(:originalList))
Then when you invoke it, you can do something like this:
Collection<String> originalList = Arrays.asList("Kenobi", "Skywalker", "Windu");
query.setParameter("originalList", originalList);
By this, you can still ensure the query will only contain items in your original list and not any possible new item from the database, provided that that last name is a candidate key in the database, otherwise I would recommend that you use the ID for the subquery instend of the last name.
if you have jpa+hibernate you can use entityManager.createQuery() for creating hql query
like that:
String hql = "UPDATE Supplier SET name = :newName WHERE name IN :name";
entityManager.createQuery(hql);