I have a list of items , those items have a sortOrder number and a name
I want to show it sorted by sortOrder but this is not working
<iron-data-table id="entriesList"
as="item"
details-enabled
items="{{entries}}">
<data-table-column name="Media" width="360px" flex="2" sort-by="item.sortOrder"> [[item.name]]
</data-table-column>
<data-table-column name="Skip" flex="0">
<paper-checkbox checked="{{item.skip}}"></paper-checkbox>
</data-table-column>
</iron-data-table>
.
Firstly, it should be:
<data-table-column name="Media" width="360px" flex="2" sort-by="sortOrder"> [[item.name]]
</data-table-column>
Secondly, it won't autosort items for you, you need to sort entries by yourself, something like:
someFuncThatGeneratesEntries: function(entries) {
return entries.sort((a, b) => Math.sign(a.sortOrder - b.sortOrder));
}
Related
I have seen related questions and answer but none of those helped me.
I have created a linq query to get an IEnumerable list like this -
**price** **car_name** **location**
23 carA locationA
43 carA locationB
56 carA locationC
12 carB locationA
34 carB locationB
46 carB locationC
Now I want only rows with minimum price of each car...
Thanks in advance.
EDIT : Sorry I wasn't clear about my question but I want all the location too...I show that in a separate dropdown list.
You need to Group the Collection based on Car Name and then Order each group by price so that you can the minimum.
var result = carList.GroupBy(x=>x.Name)
.Select(x=>x.OrderBy(p=>p.Price).First());
If you want to avoid the Order By, you could make use of Aggregate.
var result = list.GroupBy(x => x.Name)
.Select(g => g.Aggregate((c1, c2) => c1.Price < c2.Price ? c1 : c2));
Aggregate would iterate only one time over the collection, keeping track of the car with minimum price.
In either case, output sample,
from cs in myContext.CarStore
group cs by cs.car_name into cgroup
select new {
Car_Name= cgroup.Key,
Price= cgroup.Min(cs=> cs.price)});
i think this may help..
Others came up with the idea to group your cars into groups with same CarName. Then they order all CarPrices in each group, after which they take the FirstOrDefault CarPrice.
It is a bit of a waste to Order all CarPrices if you only want the minimum CarPrice
For this I use the GroupBy overload with KeySelector, ElementSelector and ResultSelector
var cheapestCars = allCars.GroupBy(
// key selector:
car => car.CarName,
// element selector
car => car.CarPrice,
// result selector:
(carName, pricesOfCarsWithThisCarName) => new
{
CarName = carName,
LowestAvailablePrices = pricesOfCarsWithThisCarName.Min(),
});
I have a list of names in NameList list .
I want to filter it and one more list of objects "NameObject" . i able to achieve in the following , but I want to avoid for loop , Is there any better way of achieving this.
foreach (string name in NamesList)
{
var find = context.Names.Single(x => x.PersonName == name);
NameObject.Add(find);
}
You can use this query:
NameObject = context.Names.Where(n => NamesList.Contains(n.PersonName)).ToList();
I have a database that contains over 10,000 U.S. zipcodes whats the best way to put these zipcodes in order? right now they are all shuffles around like : 10201,45089,32809 and I would like them to be in order from smallest to biggest like 10201,32809,45089 etc. They are under the field of "zip"
var getinfo = sqlConnection.Query<zipcodes>("Select * from zipcodes ORDER BY zip DESC").ToList();
foreach (var item in getinfo)
{
db.Entry(item).State = EntityState.Modified;
db.SaveChanges();
}
Thats the code I have above and it is not ranking properly is there something else im missing? property zip is a float.
Descending would put them from largest to smallest. What you want is to sort by ascending.
This can be done with linq:
var sortedZipCodeList = getinfo.OrderBy(x => x.Id);
Id would be whatever property is in the zipcodes class that your trying to sort by.
(for descending order the syntax is: .OrderByDescending(x => x.Id))
How do I write this linq query:
List<List<string>> listOfLists = new List<List<string>>();
listOfLists.Add(new List<string>(){"Item1", "Item2"});
listOfLists.Add(new List<string>() { "Item2", "Item2" });
//Does listOfLists contain at least one list that has one or more items?
It sounds like you're trying to find if any list has any items. Two ways of doing that:
As described, using Enumerable.Any at both levels (once with a predicate and once without):
var any = listOfLists.Any(list => list.Any());
Just flatten it and see if there are any items at all, as if there is at least one item, it must belong to a list with at least one item:
var any = listOfLists.SelectMany(list => list).Any();
I have a large unsorted list of items. Some items are important and need to listed first, followed by unimportant items. The items should be sorted by name in the two groups. I have a solution, but I believe it can be optimized. First, it gets a list of important items. Then a list of everything else, then concatenates the results. Any suggestions on how to optimize this?
Here is a simplified version of the problem for LINQPad:
var doc = XDocument.Parse(#"
<items>
<item id='a'>not important4</item>
<item id='b'>important2</item>
<item id='c'>not important2</item>
<item id='d'>not important3</item>
<item id='e'>important1</item>
<item id='f'>not important1</item>
</items>");
// identify which items are important
string[] importantItemIDs = new string[] { "b", "e" };
var items = doc.Root.Elements("item");
// get a list of important items (inner join)
var importantList = from itemID in importantItemIDs
from item in items
orderby (string) item.Value
where itemID == (string) item.Attribute("id")
select item;
// get items that are not important items
var notImportantList = items.Except(importantList).OrderBy(i => (string) i.Value);
// concatenate both sets of results into one list
var fullList = importantList.Concat(notImportantList);
fullList.Select(v => v.Value).Dump();
Here's the correct output:
important1
important2
not important1
not important2
not important3
not important4
One approach that immediately comes to mind is to utilize OrderBy as well as ThenBy to avoid querying the original data source multiple times. Something like:
var list = items
.OrderBy(i => importantItemIDs.Contains(i.Attribute("id") ? 0 : 1)
.ThenBy(i => i.Value);
.Select(i => i.Value);
I'm not sure if the ternary operator is necessary there - I forget how OrderBy deals with boolean results. Shouldn't be a major performance concern anyway and is potentially a bit clearer.
var prioritized =
from item in items
select new {
Importance = importantItemIDs.Contains((string) item.Attribute)? 1 :2,
Item = item
};
var fullList = from pitem in prioritized
orderby pitem.Importance, pitem.Item.Value
select pitem.Item.Value;