I have the following code to construct a SORT query inside a foreach loop, however my problem is this will replace my old sort descriptor with latest one.
SearchDescriptor<MyDTO> nQuery = new SearchDescriptor<MyDTO>();
foreach (var sort in criteria.SortQuery.OrderBy(o => o.SortPreference))
{
nQuery= nQuery.Sort(s => s.Field(sort.SortName, sort.SortOrder));
}
How to achieve this inside a ForEach
You can do this by
var sortDescriptor = new SortDescriptor<Document>();
foreach (var sort in sortCollection)
{
SortOrder sortOrder;
var tryParse = Enum.TryParse(sort.Order, out sortOrder);
if(!tryParse) up to you how you are going to handle incorrect sort order
sortDescriptor.Field(sort.FieldName, sortOrder);
}
client.Search<Document>(s => s.Size(0).Sort(sort => sortDescriptor));
Hope it helps.
Related
I have a list with nested items and I want to move values/node from Category1 to Category2 which are at same level. Doing it using a double for loop takes lot of time.
How can I simplify and make it fast using LINQ?
foreach (var item in masterlist) {
foreach (var item1 in item.Category1) {
item1.Category1 = item1.Category2;
item1.Category2 = null;
}
}
You still need to use a foreach because Linq is only concerned with iteration and querying and should not be used to introduce side-effects or actions (this is why Linq doesn't have a ForEach or Do extension method).
Note that because item.Category1 is overrwritten inside the loop you need to eagerly-evaluate the Linq expression first.
Try this (assuming your list-item type is named ListItem):
List<ListItem> allListItems = masterList
.SelectMany( li => li.Category1 )
.ToList();
foreach( ListItem item in listItems )
{
item.Category1 = item.Category2;
item.Category2 = null;
}
I have the following query that groups locations and the average item cost and I would like to write it as one query but I cannot figure out the syntax. What LINQ do I need to do this? I have tried writing it different ways but the syntax is not correct.
var joinedData =
from r in shops
join i in items on r.shopId equals i.shopId
select new
{
Region = r.Location,
ItemCost = i.ItemCost
};
var AverageCostByLocation = joinedData
.GroupBy(m => new { m.Location})
.Select(m => new
{
Location= m.Key.Location,
AverageItemCost = m.Average(x => x.ItemCost)
});
Well, if you put first expression in parenthesis it should allow to join both expressions as they are. Also I'd probably get rid of second anonymous type for perfomance reasons (the new { m.Location} line is redundant, you might want to use .Key instead) :
var AverageCostByLocation =
(from r in shops
join i in items on r.shopId equals i.shopId
select new
{
Region = r.Location,
ItemCost = i.ItemCost
})
.GroupBy(m => m.Location)
.Select(m => new
{
Location= m.Key,
AverageItemCost = m.Average(x => x.ItemCost)
});
I have below code in c# 4, where I am trying to use linq for ordering, grouping.
IList<Component> components = Component.OrganizationalItem.OrganizationalItem.Components(true);
IEnumerable<Component> baggage = components.Where(x => x.IsBasedOnSchema(Constants.Schemas.BaggageAllowance.ToString()))
.OrderBy(x => x.ComponentValue("name").StringValue("Code"))
.GroupBy(x => x.ComponentValue("name").StringValue("Code"));
In above sample when I am trying to use GroupBy it is giving error, please see below:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string,Tridion.ContentManager.ContentManagement.Component>>' to 'System.Collections.Generic.IEnumerable<Tridion.ContentManager.ContentManagement.Component>'. An explicit conversion exists (are you missing a cast?)*
The result of GroupBy will be an IGrouping<string, Component> - it's a sequence of groups of components, rather than one sequence of components. That's the whole point of grouping. So this should be fine:
IEnumerable<IGrouping<string, Component>> baggage = ... query as before ...;
Or just use implicit typing:
var baggage = ...;
You can then iterate over the groups:
foreach (var group in baggage)
{
Console.WriteLine("Key: {0}", group.Key);
foreach (var component in group)
{
...
}
}
I have a cluttery piece of code that I would like to shorten using Linq. It's about the part in the foreach() loop that performs an additional grouping on the result set and builds a nested Dictionary.
Is this possible using a shorter Linq syntax?
var q = from entity in this.Context.Entities
join text in this.Context.Texts on new { ObjectType = 1, ObjectId = entity.EntityId} equals new { ObjectType = text.ObjectType, ObjectId = text.ObjectId}
into texts
select new {entity, texts};
foreach (var result in q)
{
//Can this grouping be performed in the LINQ query above?
var grouped = from tx in result.texts
group tx by tx.Language
into langGroup
select new
{
langGroup.Key,
langGroup
};
//End grouping
var byLanguage = grouped.ToDictionary(x => x.Key, x => x.langGroup.ToDictionary(y => y.PropertyName, y => y.Text));
result.f.Apply(x => x.Texts = byLanguage);
}
return q.Select(x => x.entity);
Sideinfo:
What basically happens is that "texts" for every language and for every property for a certain objecttype (in this case hardcoded 1) are selected and grouped by language. A dictionary of dictionaries is created for every language and then for every property.
Entities have a property called Texts (the dictionary of dictionaries). Apply is a custom extension method which looks like this:
public static T Apply<T>(this T subject, Action<T> action)
{
action(subject);
return subject;
}
isn't this far simpler?
foreach(var entity in Context.Entities)
{
// Create the result dictionary.
entity.Texts = new Dictionary<Language,Dictionary<PropertyName,Text>>();
// loop through each text we want to classify
foreach(var text in Context.Texts.Where(t => t.ObjectType == 1
&& t.ObjectId == entity.ObjectId))
{
var language = text.Language;
var property = text.PropertyName;
// Create the sub-level dictionary, if required
if (!entity.Texts.ContainsKey(language))
entity.Texts[language] = new Dictionary<PropertyName,Text>();
entity.Texts[language][property] = text;
}
}
Sometimes good old foreach loops do the job much better.
Language, PropertyName and Text have no type in your code, so I named my types after the names...
I'm using LINQ to manipulate a datatable. I have 3 columns - I would like group by one and then select the remaining 2 columns together. At the moment I have something like this
var query = reportDataTable.AsEnumerable()
.GroupBy(c => c["Code"])
.Select(g =>
new {
Code = g.Key,
Rank = g.Select(f => new
{ f["rank"],
f["Name"]}).ToArray()
});
but I get issues due to anonymous types. I know this syntax would work if I could reference the the column headers directly (in say a list or w/e). How can I get around this with DataTables? Cheers.
Edit:
Well I'd like to be able to reference the fields later when I come to populate the data into a different datatable:
foreach (var q in query)
{
DataRow df = dp.NewRow();
df["Code"] = q.Code;
foreach (var rank in q.Rank)
{
df[rank.name] = rank.rank;
}
dp.Rows.Add(df);
}
define your Rank fields, Also if you have a class for it, call related class constructor,
you can see this in bellow code, before ToArray.
var query = reportDataTable.AsEnumerable()
.GroupBy(c => c["Code"])
.Select(g =>
new { Code = g.Key, Rank =
g.Select(f => new { rank = f["rank"], name = f["Name"]})
.ToArray() });