I have the following code, that creates a dictionary:
branches.ToDictionary(row => row.Field<object>(1), row => row.Field<object>(3)).ToList();
I want to create the dictionary value as row.Field<object>(3) + row.Field<object>(4). I think I'm looking for some kind of a concat method, but none seem to be available. Will I have to loop through each element individually?
If the types of row.Field<object>(3) and row.Field<object>(4) are string, then read them as strings and + them together (or perhaps use string.Format or string.Concat):
branches.ToDictionary(
row => row.Field<object>(1),
row => row.Field<string>(3) + row.Field<string>(4)
).ToList();
If they are really object or two types that you just want paired together, you can add them as a tuple, or preferably create a class to hold them. For example:
public class IntAndString //Choose a better name than this!
{
public int IntValue { get; set; }
public string StringValue { get; set; }
}
And then project the values into the class:
branches.ToDictionary(
row => row.Field<object>(1),
row => new IntAndString
{
IntValue = row.Field<int>(3),
StringValue = row.Field<string>(4)
}
).ToList();
Just concat them, but if these are string-columns use Field<string>(preferred) or ToString:
branches.ToDictionary(
row => row.Field<object>(1),
row => row.Field<string>(3) + row.Field<string>(4))
.ToList();
In general use the correct type instead always object.
Another way if you only have objects is to use String.Concat:
branches.ToDictionary(
row => row.Field<object>(1),
row => String.Concat(row.Field<object>(3), row.Field<object>(4)))
.ToList();
Related
I am just not understanding the LINQ non-query syntax for GroupBy.
I have a collection of objects that I want to group by a single property. In this case Name
{ Id="1", Name="Bob", Age="23" }
{ Id="2", Name="Sally", Age="41" }
{ Id="3", Name="Bob", Age="73" }
{ Id="4", Name="Bob", Age="34" }
I would like to end up with a collection of all the unique names
{ Name="Bob" }
{ Name="Sally" }
Based on some examples I looked at I thought this would be the way to do it
var uniqueNameCollection = Persons.GroupBy(x => x.Name).Select(y => y.Key).ToList();
But I ended up with a collection with one item. So I though maybe I was over complicating things with the projection. I tried this
var uniqueNameCollection = Persons.GroupBy(x => x.Name).ToList();
Same result. I ended up with a single item in the collection. What am I doing wrong here? I am just looking to GroupBy the Name property.
var names = Persons.Select(p => p.Name).Distinct().ToList()
If you just want names
LINQ's GroupBy doesn't work the same way that SQL's GROUP BY does.
GroupBy takes a sequence and a function to find the field to group by as parameters, and return a sequence of IGroupings that each have a Key that is the field value that was grouped by and sequence of elements in that group.
IEnumerable<IGrouping<TSource>> GroupBy<TSource, TKey>(
IEnumerable<TSource> sequence,
Func<TSource, TKey> keySelector)
{ ... }
So if you start with a list like this:
class Person
{
public string Name;
}
var people = new List<Person> {
new Person { Name = "Adam" },
new Person { Name = "Eve" }
}
Grouping by name will look like this
IEnumerable<IGrouping<Person>> groups = people.GroupBy(person => person.Name);
You could then select the key from each group like this:
IEnumerable<string> names = groups.Select(group => group.Key);
names will be distinct because if there were multiple people with the same name, they would have been in the same group and there would only be one group with that name.
For what you need, it would probably be more efficient to just select the names and then use Distinct
var names = people.Select(p => p.Name).Distinct();
var uniqueNameCollection = Persons.GroupBy(x => x.Name).Select(y => y.Key).ToList();
Appears valid to me. .net Fiddle showing proper expected outcome: https://dotnetfiddle.net/2hqOvt
Using your data I ran the following code statement
var uniqueNameCollection = people.GroupBy(x => x.Name).Select(y => y.Key).ToList();
The return results were List
Bob
Sally
With 2 items in the List
run the following statement and your count should be 2.
people.GroupBy(x => x.Name).Select(y => y.Key).ToList().Count();
Works for me, download a nugget MoreLinq
using MoreLinq
var distinctitems = list.DistinctBy( u => u.Name);
I have this query that hasn't changed since I first got it working:
ISearchResponse<Series> response = await IndexManager.GetClient()
.SearchAsync<Series>(r => r
.Filter(f => f.Term<Role>(t => t.ReleasableTo.First(), Role.Visitor))
.SortDescending(ser => ser.EndDate)
.Size(1));
My IndexManager.GetClient() is simply responsible for setting up my connection to ElasticSearch, and ensuring that the indexes are built properly. The rest of the code gets the most recent article series that is releasable to the general public.
Inside the IndexManager I set up explicit index mapping, and when I did that I got results from my query every time. The code looked like this:
client.Map<Series>(m => m.Dynamic(DynamicMappingOption.Allow)
.DynamicTemplates(t => t
.Add(a => a.Name("releasableTo").Match("*releasableTo").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
.Add(a => a.Name("id").Match("*id").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
.Add(a => a.Name("services").Match("*amPm").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed)))
.Match("*dayOfWeek").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
.Add(a => a.Name("urls").Match("*Url").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
));
While all well and good, doing this for every type we stored wasn't really going to scale well. So I made a conscious decision to use the attributes and map it that way:
// In IndexManager
client.Map<T>(m => m.MapFromAttributes());
// In the type definition
class Series
{
// ....
[DataMember]
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true)]
public HashSet<Role> ReleasableTo { get; set; }
// ....
}
As soon as I do this, I no longer get results. When I look at my indexes in Kibana, I see my 'releasableTo' field is not analyzed and it is indexed. However the query I wrote no longer works. If I remove the filter clause I get results, but I really need that to work.
What am I missing? How do I get my query to work again?
It appears that the ElasticSearch attributes to provide indexing hints don't know what to do with enums.
The problem turned out to be the fact that the Role type was an enumeration. The client.Map<Series>(m => m.MapFromAttributes()) call skipped that property. At run time, it dynamically maps the property to a string.
// In the type definition
class Series
{
// ....
[DataMember]
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true)]
public HashSet<Role> ReleasableTo { get; set; }
// ....
}
To get the field properly indexed I had to explicitly set it's type in the ElasticProperty attribute. Changing the code to this:
// In the type definition
class Series
{
// ....
[DataMember]
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String, Store = true)]
public HashSet<Role> ReleasableTo { get; set; }
// ....
}
made my query work again. The moral of the story is that unless it's a primitive type, be explicit when setting the field type.
I have a table (or entity) named Cases. There is another table CaseStatus_Lookup and the primary key of this table is a foreign key in the Cases table.
What I want to do is: For every status type I want the number of count of cases. For e.g. if status = in progress , I want to know how many cases are in that status.
one other thing: I also want to filter the Cases based on UserID.
I tried several ways in LINQ but could not get vary far. I was wondering if someone could help.
try Linq .GroupBy
am assuming your entity structure
suppose your Case Entity is like
public class Case
{
public int Id{get;set;}
public int CaseStatusId{get;set;}
public int UserId{get;set;}
//navigational fields
public virtual CaseStatus CaseStatus {get;set;}
}
and suppose your CaseStatus entity is like:
public class CaseStatus
{
public int Id{get;set;}
public string Name{get;set;}
//navigational fields..
public virtual ICollection<Case> Cases{get;set;}
}
then you can do this:
using (myDbContext db = new myDbContext())
{
var query = db.Cases.GroupBy(case => case.CaseStatus.Name)
.Select(group =>
new {
Name = group.Key,
Cases= group.OrderBy(x => x.Id),
Count= group.Count()
}
).ToList();
//query will give you count of cases grouped by CaseStatus.
}
similarly you can further filter your result based on userId.
Start to explore about Linq .GroupBy
You need a function that returns the sum and takes the status as parameter :- something like below.
MyCaseStatusEnum caseStatus; //Pass your required status
int caseCount = myCases
.Where(r => r.Status == caseStatus)
.GroupBy(p => p.Status)
.Select(q => q.Count()).FirstOrDefault<int>();
I had the following query using normal linq and it was working great (using anonymous type),
var result = from s in Items
group s by s.StartTime into groupedItems
select new {groupedItems.Key, Items= groupedItems.OrderBy(x => x.Name) };
But using Dynamic Linq I cannot get it to order by within the groupby.
result = Items.GroupBy("StartTime", "it").OrderBy("Name");
It states the Name isn't available. It is worth noting that if I take my OrderBy off, everything works great but items inside each "Key" are not ordered.
This is a good question!
I simulated your situation by creating a class called Item.
public class Item
{
public DateTime StartTime { get; set; }
public string Name { get; set; }
}
and then created a basic list of items to do the groupby.
List<Item> Items = new List<Item>()
{
new Item() { StartTime = DateTime.Today, Name = "item2"},
new Item() { StartTime = DateTime.Today, Name = "item1"},
new Item() { StartTime = DateTime.Today.AddDays(-1), Name = "item3"},
};
Now the big difference in the 2 queries is where the order by is being performed. In the first query, when you perform groupedItems.OrderBy(x => x.Name) its being performed on a IGrouping<DateTime,Item> or a single entry as it iterates through all the groupings.
In the second query, the orderby is being performed after the fact. This means you're doing an orderby on a IEnumerable<IGrouping<DateTime,Item>> because the iterations have already happened.
Since Microsoft was nice they added something to help deal with this for expressions. This overload allows you to specify the item returned as it iterates through the collection. Here's an example of the code:
var expressionResult = Items.GroupBy(x => x.StartTime,
(key, grpItems) => new { key, Items = grpItems.OrderBy(y => y.Name) });
The second part of the GroupBy you can specify a lambda expression that takes a key and a grouping of items under that key and return an entry that you specify, which is the same as you're doing in the original query.
Hope this helps!
I have the following collection
public IQueryable<myObjectType > GetWorkCellLoadGraphDataByIdDummy()
{
IList<myObjectType> workCellLoadGraphDataCollection = new List<myObject>()
{
new myObjectType(DateTime.Today.AddHours(8).AddMinutes(30), 1),
new myObjectType(DateTime.Today.AddHours(10).AddMinutes( 10 ), 6 ),
new myObjectType(DateTime.Today.AddHours(13).AddMinutes( 30 ),8 ),
new myObjectType(DateTime.Today.AddDays(1).AddHours(8).AddMinutes(30), 1),
new myObjectType(DateTime.Today.AddDays(1).AddHours( 10 ).AddMinutes( 10 ), 5 ),
new myObjectType(DateTime.Today.AddDays(1).AddHours( 13 ).AddMinutes( 30 ), 2 )
};
// Write some LINQ code to group data according to first parameter
// Perform sum of last parameter
// Shape the data to be in the form of myObjectType
// return result;
}
and what I want to do is to group items by the first parameter of the myObjectType class.
Then for each grouping, I'd like to do the sum of all the last parameters.
Finally the result should be returned in the form of "myObjectType"
I know how to do it the old fashioned way i.e. looping through all the items and doing the sums. However, I'd like to learn how to do it in LINQ which is something I've just started.
Can anyone point me in the right direction so that I can translate my requirements into LINQ?
In effect, the result should be a collection containing two objects of type myObjectType as follows:
First object in collection is (DateTime.Today, 15 )
Second object in collection is (DateTime.Today.AddDays(1), 8)
TIA,
David
Given a class with this basic design
class MyObjectType
{
public MyObjectType(DateTime date, int count)
{
this.MyDate = date;
this.MyCount = count;
}
public DateTime MyDate { get; set; }
public int MyCount { get; set; }
}
You could fulfill your requirement using LINQ in the manner below. The first example produces IEnumerable<MyObjectType> using fluent extension method syntax.
var query = collection.GroupBy(obj => obj.MyDate.Date)
.Select(grp =>
new MyObjectType(grp.Key, grp.Sum(obj => obj.MyCount))
);
The second version achieves the same result but uses the more SQL-esque query expression syntax.
var query = from obj in collection
group obj by obj.MyDate.Date into grp
let mySum = grp.Sum(item => item.MyCount)
select new MyObjectType(grp.Key, mySum);
From there, you utilize the extension methods AsQueryable to yield an IQueryable result, or ToList() / ToArray() to yield concrete collections.