I have a feature class which contain property information, what I would like to do it total the value of all properties and display that in a table. The attribute table looks something like this:
ObjectId PID Value
0 1000 10,000
1 1001 25,000
2 1002 100,000
I would like to sum the value field, in order to give me a total for the entire area. So in this example, the value of all properties would be $135,000... I would like to calculate this and display it...
sounds like a pretty straight forward problem, but I can't seem to find a solution. How to do this on Xamarin forms MVVM format
Well, assuming you are using SQL then you would use something like:
select sum(count) from TableXXX where location = 'colombo'.
If you are using EF then it would be more like:
double x = dbContext.DbSet("XXX")
.Where(item => item.Location == "colombo")
.Select(item => item.Count)
.Sum();
Related
I am trying to get one row per id from a DataTable, and I do not care which row I take. The same id can exist on several rows in the table.
Here's the expression that's giving me trouble:
dt.AsEnumerable().GroupBy(i => i.Field<int>("id")).Select(i => i.First())
Running just this section dt.AsEnumerable().GroupBy(i => i.Field<int>("id") correctly gives me a result of 22 groupings for my DataTable. (I have 22 ids with data in this table)
However, when adding on the .Select(i => i.First()), I am only seeing 10 data rows.
To me this doesn't seem to make any sense. If the GroupBy function managed to find 22 distinct id values, I would expect this logic to grab one of each.
My only other thought is that maybe it's just a weird side effect of viewing this data through a watch in Visual Studio rather than assigning to a variable.
If you think it's just weird side effects of viewing the data in a watch, which can happen with LINQ statements, then split it out into
var groups = dt.AsEnumerable().GroupBy(i => i.Field<int>("id")).ToList();
var firstOfGroups = groups.Select(i => i.First()).ToList();
and then look at groups and firstOfGroups in the debugger. Temporarily evaluating items with .ToList() can help a lot with viewing things in the debugger.
I think it is possible, can double check the count of each group items
.Select(g=>new { k = g.Key, c = g.Count() })
Let's say I have a query that returns something simple like...
**TYPE** **TOTAL**
A 2
B 4
And I want to do some math like A/(A+B) -> 2/(2+4) then display that on my website. Is that more feasible to do within the query or after when I have it stored in a datatable??
Just try something like this if you realy need to get it done in same way.
SELECT A/(A+B) FROM((SELECT total as A FROM TB WHERE type='A' LIMIT 1),(SELECT total as B FROM TB WHERE type='B' LIMIT 1))
Tested and working..
Hi all and thanks in advance, I'm stumped on how to use a where clause with the conditional statement that I need to execute. I have two grids of information, one grid is dependent on the other for determining what shows. In the first grid the Date field can be a date or it can say Never.
The first grid's data is like so:
ID Date Title
--- ----- ------
12 Never Home
13 Never School
14 Never Work
The second grid will only show one row of the three depending on what the value of the Date field is, in this example it should be:
ID Date Title
--- ----- ------
12 Never Home
This information is pulled into a List that I want to iterate through using LINQ. What I want to achieve is:
If(All Date values == 'Never')
Then pull the first one (12)
else
if(Date has value)
then pull the first that has a date
myList.Where(??what goes here??).Select(t => t).FirstOrDefault();
var record = myList.FirstOrDefault(m => !m.Date.Equals("Never"))
?? myList.FirstOrDefault();
i.e. first one that doesn't equal never or null, and if null, just the first one (or null).
You're probably looking for something like this:
var record = myList.All(m => m.Date.Equals("Never"))
? myList.FirstOrDefault()
: myList.FirstOrDefault(m => !m.Date.Equals("Never"));
For more on .All() just look at this MSDN post.
Since you have two rules, you will need an if condition somewhere.
The simplest form that I can think of is something like this:
return myList.All(x => x.Date == "Never") ? myList.FirstOrDefault() : myList.FirstOrDefault(x => x.Date != "Never");
I have a sql table called expenses with an int column called cost. In my application this data is displayed on a grid which is refreshing every time I insert a new row with a linq2sql insert. What I would like to do is have an integer variable in my application that is the sum of all the fields in the cost column every time I insert a row.
Is there a simple way to sum these fields with linq2sql every time I do an insert. Please try to avoid lambda as I haven't gotten to learning that yet.
Thanks!
Assuming that you use query syntax instead of lambdas, here you are:
var totalCost = (from expensesRow in dataContext.Expenses
select expensesRow.cost)
.Sum();
Which in fact is the same as:
var totalCost = dataContext.Expenses
.Sum(x => x.cost);
Here dataContext is an instance of your Linq2Sql DataContext class.
You get to learn lambdas today. http://www.theabsentmindedcoder.com/2010/06/linq-sum.html has exactly what you want, and uses a very simple lambda to get there. You could get away without the lambda by making your select gather up only the one column of integers you're trying to sum, but why do extra work to not learn things?
Say I had a table called users with the following structure:
[Table users] - id | username | points_awarded
Then, I can find the total number of points I awarded every user by running the query:
SELECT SUM(points_awarded) as total_points FROM users
You can also count how many users have points greater than our equal to N. For example, says N = 500, then I can run:
SELECT COUNT(id) as num_users_with_points FROM users WHERE points_award >= 500
Check out:
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
http://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html
for more information.
I'm trying to use paging in conjunction with a sum projection to get a sum of the values in a column for just the page of results I'm interested in. I'm using .NET, C# and NHibernate 3.1
I have an ICriteria to start with which is related to all rows from the associated db table.
I'm then doing the following to get a version with the first page (say, 10 items out of 40):
ICriteria recordsCriteria = CriteriaTransformer.Clone(criteria);
recordsCriteria.SetFirstResult(0);
recordsCriteria.SetMaxResults(10);
I'm using this ICriteria for something else so I then create two further clones:
ICriteria totalAggCriteria = CriteriaTransformer.Clone(criteria);
ICriteria pageAggCriteria = CriteriaTransformer.Clone(recordsCriteria);
If I take a look inside these two new ones the first has 40 items in and the second has 10 - exactly what I want.
Let's say the objects coming back from the DB have a column called "ColA" and it's of type Int32.
From this, I want the sum of all 40 ColA values and the sum of the first 10 ColA values.
To get the sum of all 40 ColA values, I do the following:
totalAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum("ColA"));
var totalSum = totalAggCriteria.UniqueResult();
The value in totalSum is correct.
To get the sum of the first 10 ColA values, I'm trying the following:
pageAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum("ColA"));
vat pageSum = pageAddCriteria.UniqueResult();
However, this gives me the same value as the previous one - for all 40 ColA values.
I've also tried the following but it gives the same result:
pageAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum(column));
pageAggCriteria.SetFirstResult(firstResult.Value);
pageAggCriteria.SetMaxResults(pageSize.Value);
pageSum = pageAggCriteria.UniqueResult();
And also:
pageAggCriteria.SetFirstResult(firstResult.Value);
pageAggCriteria.SetMaxResults(pageSize.Value);
pageAggCriteria.SetProjection(NHibernate.Criterion.Projections.Sum(column));
pageSum = pageAggCriteria.UniqueResult();
Can anyone give an idea on where I'm going wrong and how I can actually get the sum of the ColA values in the first 10 results?
Thanks
Probably easiest to do that sum client side. The aggregate function is operating on the whole table. What you are trying to do is run the aggregate function against the paged result which I don't think is possible with NH.
In other words, you want select sum(colA) from (select top 10 ...) but that criteria will give you select top 10 sum(colA) from ...)