I'm trying to show my items in pagination method.
the first part happened by grouping them by i don't know how can sort them too .
in code Item.page contain each item page number and Item.priority contain
order of item.
now how can i sort by order too ?
this is my code :
"#model IEnumerable<MyProgram.Models.Question>"
foreach (var group in Model.GroupBy((item => item.Page)))
{
<fieldset>
<legend></legend>
#{
foreach (var item in group)
{
if (item.Type == "label")
{
i tired this but nothing came up after item. :
foreach (var group in Model.GroupBy((item => item.Page)).OrderBy(item => item.))
This shall work:
Model.GroupBy(item=>item.page)
.Select(group=>group.OrderBy(I=>I.priority))
You need each group to be ordered not the entire grouped enumerable.
try in second foreach
foreach (var item in group.OrderBy(i => i.priority))
if your only goal is to achieve sorting by two or more parameters use OrderBy(...).ThenBy(...)
Related
I am trying to filter a result set which I read from a Sharepoint(list). I have to filter records that have a user specified and sadly user column of the list is a lookup column and gets the actual username from another list.
Normally,I can get the username value of a record (if it has any) like this:
foreach (var item in listItems)
{
if (item.FieldValues.TryGetValue("nqhs", out var userLookup))
{
if (userLookup != null && userLookup.ToString() == "Microsoft.SharePoint.Client.FieldUserValue")
{
var theUser = ((FieldLookupValue)item["nqhs"]).LookupValue; //this returns theUser = "John Doe"
}
}
}
nqhs is the name of the column which holds the Username in the Sharepoint list.
Is there any way I can apply this logic to a where clause in foreach, so that I can work with a much smaller result set?
I am trying something like this but I can't get it to work:
foreach (var item in listItems.Where(p => p.((FieldLookupValue)p["nqhs"]).LookupValue == "John Doe"))
Also tried this and get and "object reference not set to an instance of an object" error...
foreach (var item in from item in listItems where ((FieldLookupValue)item["nqhs"]).LookupValue == "John Doe" select item)
Try filtering the lookup list first, and then use the filtered list in you foreach
var filteredList = listItems.Where(p => p.((FieldLookupValue)p["nqhs"]);
foreach(var item in filteredList)
Some queries to SP require an execution to return results before they can be used. It's also better for performance because the Where clause in your current loop will execute with each iteration.
I want to update vmlist by geting values from vlist without using any foreach loop.
For now I am just doing this with foreach loop, but I want to replace this foreach with LINQ
foreach (var item in vlist){
vmlist.Where(list => list.SId==item.SId && list.ParameterId==item.ParameterId && list.WId==item.WId)
.Select(li => { li.Value = item.Value; return li; }).ToList();
}
Your current approach is very inefficient - it's O(N * M) and it creates a list on each iteration.
Using a join would be more efficient - I would still use a foreach loop, but separate the querying part from the update part:
var pairsToUpdate = from original in vmlist
join item in vlist
on new { original.SId, original.ParameterId, original.WId }
equals new { item.SId, item.ParameterId, item.WId }
select new { original, item };
foreach (var pair in pairsToUpdate)
{
pair.original.Value = pair.item.Value;
}
No abuse of Select with side-effects
No extra lists created for no good reason
More efficient selection of items to update
Is there a simple solution to remove specific itemS from ListView.SelectedItems?
I've a ListView bound to an ObservableCollection<MyClass> (MyClass has some attributes e.g. Name).
Something like:
mylistview.SelectedItems.Remove(FROM myClassItem IN mylistview.SelectedItems WHERE myClassItem.Name == "test");
Of course, it doesn't work.
foreach (var item in mylistview.SelectedItems
.Cast<ListViewItem>()
.Where(lvi => lvi.Name == "test")
item.Remove();
I am selecting the following data from an SQL database table in my controller
var fixtures = from f in _context.Fixtures
where f.MatchDate.Year == year &&
(gender.Contains( f.Gender )) &&
(type.Contains( f.MatchType )) &&
(team.Contains( f.TeamName ))
orderby f.TeamName, f.MatchDate
select f;
return View( "DisplayLeagues", fixtures.ToList() );
and calling the DisplayLeagues view to display it. As you can see from the orderby, it contains more than one team. I want to display a separate table on the webpage for each team. My page declaration contains
Inherits="System.Web.Mvc.ViewPage<IEnumerable<RLSBCWebSite.Domain.Entities.Fixture>>"
as the declaration of the Model. To display the individual lines of the table, I have
<% foreach (var item in Model.AsEnumerable()) { %>
But how do I add a loop control based on the name of the team around the table as a whole? It doesn't seem possible to code
<% foreach (var team in Model.TeamName ....
Something like:
group g by f.TeamName into tmp
select new {TeamName = f.Key, Fixtures = tmp.ToList() }
this will give you one group per team, i.e.
foreach(var grp in query) {
Console.WriteLine(grp.TeamName);
foreach(var fixture in grp.Fixtures) {
// write fixture details
}
}
Note that this changes each item to an IGrouping<string, List<Fixture>>, and note that ToLookup will do something very similar if that is easier.
For the outer loop, which produces a separate table for each team, I used the following
<% foreach (var teamName in Model.AsEnumerable().GroupBy( r => r.TeamName )) {
For the inner loop, which produces the individual matches for each team, I used
<% foreach (var item in Model.AsEnumerable().Where(item => item.TeamName == teamName.Key.ToString())) {
This works very well.
I have a list of news unsorted items, some of which have a priority flag. I need the priority items to float to the top of the list, and then sort the rest by a date.
So, the end result is a list of news items that has the priority items showing at the top and the remainder sorted by date.
There has to be a better way to do it than this but I'm not sure what the best way would be -
foreach (var newsItem in newsItems)
{
if (newsItem.isPriority)
{
addToPriorityList(newsItem);
}
else
{
addToOtherList(newsItem);
}
}
foreach (var priorityItem in priorityList)
{
addtoMainList(priorityItem);
}
OtherList.SortbyDate();
foreach (var otherItem in otherList)
{
addtoMainList(otherItem);
}
Is there a more elegant way to pull this off? I assume I could use LINQ but I'm very new to it so I'm not comfortable with the syntax.
Try this: (edited according to the suggestion in the 1st comment)
var sorteditems = newsItems.OrderByDescending(item => item.IsPriority)
.ThenBy(item => item.Date);
You should be able to do:
newsItems.OrderBy(item => item.Priority).ThenBy(item => item.Date);