Count the hits in a chained chart - c#

I'm new in LinqPad. I already solved all my problems except the following:
How can I get the .Count() of one column, respectively the list in the one cell. Like you see in the picture.
My code example is:
var test2 = (from d in DocumentTypeLabels
select new {d.DocumentTypePIMID, d.Name, d.DocumentType.Documents}
)
.Take(1); test2.Dump();
My idea was:
var test2 = (from d in DocumentTypeLabels
select new {d.DocumentTypePIMID, d.Name, (d.DocumentType.Documents).Count()}
)
.Take(1); test2.Dump();
but unfortunately this doesn't work.
Does anyone have an idea?

Given the type of DocumentType.Documents is a Collection, you could try using the Length or Count properties from you collection and name a property on your result. For sample:
var test2 = (from d in DocumentTypeLabels
select new {
d.DocumentTypePIMID,
d.Name,
DocumentsTotal = d.DocumentType.Documents.Count()
}).Take(1);
When you do not define a name for a property on the select statement, it will generate a property with a name of the property you have provided. For sample: x.Name will be Name on result.

Related

c# Linq Take 5 Object for each Group

I try Get 5 Object in each group to display.
I tried code like this:
var BBS = (from bb in db.Object
where SubjectList.Contain(bb.Type)
select new ObjectModel {
Subject = bb.Subject,
CDate = bb.CDate
}).GroupBy(a => a.BBSTypeSubject).Take(5);
but it doesn't work.
and then I try it by using Foreach
First off, realize that C# is case sensitive, so you need to spell function names like Take and Contains correctly. But you can solve this problem fairly easily by just using this overload of GroupBy instead.
var BBS = (from bb in db.Object
where SubjectList.Contains(bb.Type)
select new ObjectModel {
Subject = bb.Subject,
CDate = bb.CDate
}).GroupBy(a => a.BBSTypeSubject, (k, g) => g.Take(5));
The second parameter allows you to specify what result object will be returned for each group. In this case, I simply take the first 5 items from the group and return those instead.
When you select new ObjectModel, there's no property called BBSTypeSubject. You need to include that. So it should be
select new ObjectModel {
Subject = bb.Subject,
CDate = bb.CDate,
BBSTypeSubject= ???
}

grouping and counting in linq-to-sql

I have the following query that receives a list of IDs and I want to do a count. There's also an object model CountModel that holds the counts with each property defined as an int.
public class GetCountByStatus(List<int> TheIDs)
{
...using MyDC...
var CountData = (from d in MyDC.Data
where TheIDs.Contains(d.ID)
group d by d.Status into statusgroup
select new CountModel()
{
CountStatus1 = (from g in statusgroup
where g.Status == 1
select g).Count(),
CountStatus2 = (from g in statusgroup
where g.Status == 2
select g).Count(),
CountStatusN = ....
}).Single();
If for instance there are no elements with status N, will this code crash or will the count be 0 for CountStatusN ? Is this the best way to do what I want?
Thanks.
I would go for a dictionary instead, try something like this:
var countData = MyDC.Data.Where(y => TheIDs.Contains(y.ID))
.GroupBy(y => y.Status).ToDictionary(y => y.Key, y => y.Count());
I haven't tried it my self and not written the code in VS, but I think that is almost how you do can do it. That will give you a dictionary where the key is the status and the value is the count of that status.
Defining a model with properties named SomethingX is not very flexible. That means you have to go in an change the model when there is a new status. Keeping the data in the dictionary instead will save you from that.
Count() will always return an integer, which is 0 if there are no elements with the given status. Therefore CountStatusN will always be an integer as well.

LINQ group by not working

I've read lots of group by replies in this forum, but I have to ask anyway:
var errandQuery = (from t in db.TimereportSet
group t by new { t.Errand.Name, t.Date } into g
select new ErrandTime { Date = g.Key.Date, Value = g.Sum(e => e.Hours) }).ToList();
why isn't this working. I get the following exception: "Unknown column 'GroupBy1.K1' in 'field list'"
the exception comes from the mySQLClient.
You're not selecting Hours into g, so they aren't there to sum.
I don't know what your data looks like but try this:
EDIT:
var errandQuery = (from t in db.TimereportSet
group t by new { t.Errand.Name, t.Date } into g
select new ErrandTime { Date = g.Key.Date, Value = g.Sum**(t => t.Hours)** }).ToList();
Sorry, my first response was incorrect.
You LINQ query is correct, except right at the end --- you are using e.. where you need to reference the items you selected ... so you would need to use t in your lambda expression instead of e
I wrote this as a comment but it should be an answer, this is a confirmed bug in mySQL with LinQ. Is there another way of querying for the same thing using a work around, objectquery or direct sql or something else using the entity framework:
var errandQuery = (from t in db.TimereportSet
group t by new { t.Errand.Name, t.Date } into g
select new ErrandTime {
Date = g.Key.Date,
Value = g.Sum(e => e.Hours)}).ToList();
or should I create a new post for this question?

Linq using Select and an Indexer

I have the following query that runs successfully in LinqPad:
var results =
from container in Container
join containerType in ContainerType on container.ContainerType equals containerType
where containerType.ContainerTypeID == 2
select new { ContainerID = container.ContainerID, TypeID = container.ContainerTypeID};
results.Dump();
I would like to change the select to use an indexer so that the select would look something like this:
select new { ContainerID = container.ContainerID, TypeID = container.ContainerTypeID, ContainerIndex = index };
What I cannot seem to get right is the proper syntax for select to use the select indexer.
Thanks for your help.
You can't get the index with the query expression format, but there's an overload for Select that you can use in dot notation that will do it. You can stick to query expression format for the bulk of it, and then add the index in an extra select projection:
var tmp =
from container in Container
join containerType in ContainerType
on container.ContainerType equals containerType
where containerType.ContainerTypeID == 2
select new { ContainerID = container.ContainerID,
TypeID = container.ContainerTypeID};
var results = tmp.Select((x, index) => new { x.ContainerID, x.TypeID,
ContainerIndex = index });
I'm probably missing something, but if your items in Container already have property ContainerTypeID, I don't understand why you need the join. It appears to me that joining to ContainerType is not providing any extra properties that are required for this operation.
As such:
Container
.Where(c => c.ContainerTypeID==2)
.Select((c,i) => new {c.ContainerID, c.TypeID, Index = i})

Linq grouping .include("Table") returning null on Table

I have a linq query that is grouping by answers by QuestionGroup.
I need to have the table AssessmentQuestionsReference load so that i can bind to it in my WPF app.
var groupedAnswers = from a in App.ents.AssessmentAnswers.Include("AssessmentQuestions")
where a.Organisations.OrganisationID == App.selectedOrganisation.OrganisationID
group a by a.AssessmentQuestions.AssessmentQuestionGroups.QuestionGroup into g
select new { Group = g.Key, Answer = g };
When i drill down into g, AssessmentQuestions is "null". I am not sure why as i thought it should have loaded it even without the include as i am going through that table to get the question groups.
Any ideas?
Have you tried including AssessmentQuestions.AssessmentQuestionGroups?
Your .Include("AssessmentQuestions") will pull in a.AssessmentQuestions, but not a.AssessmentQuestions.AssessmentQuestionGroups.
I add for check alike string and include with group worked unexpected. This is strange but work
var yy = (from r in context.RateSet.Include(x => x.Currency).Include(y => y.Currency1)
select r).ToList();
var xx = (from r in context.RateSet.Include(x => x.Currency).Include(y => y.Currency1)
orderby r.DateRate, r.Currency.NameCurrency
group r by new { r.IdFromCurrency, r.IdToCurrency} into gp
select gp.FirstOrDefault()).ToList();

Categories

Resources