Linq query to get count like select 'iphone',count(*) [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
SQL statement :
select 'iphone',count(*) from tablename where id=5
Result:
Name Num
'iphone' 50
How should i do with linq to get the same result(SQL statement)?
thanks in advance.

var Result = new { Name = "iPhone", Num = yourSequence.Count(x => x.Id == 5) };

Related

Property IncreaseValue by reflection [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i'm need to know how to add a value to a property using reflection
example i have property with name Health
i want to increase the value by 10
var prop = typeof (Character).GetProperty(actionParams.PropertyName);
var oldValue = (int)prop.GetValue(client.Character);
prop.SetValue(client.Character, Convert.ChangeType((oldValue + 10), prop.PropertyType));
but i get cast error
System.InvalidCastException: Specified cast is not valid.
Most probably your property with name Health is not of type int, so the cast (int)prop.GetValue(client.Character); fails.

Can't compare int and IQueryable<int> and need all rows, not FirstOrDefault() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am getting a type error. Cannot compare int and IQueryable
I have looked for help on this, but have only seen info on .FirstOrDefault() and single and other methods to return on result row. The issue I am having is that I want all rows where the product p matches the subcategory. Normally it doesnt seem to be an issue when grabbing multiple rows, but while it seems to be common, I cant figure it out
var subcategoryId = someNumber;
productDetails =
from p in db.Products
where p.SubCategoryId == subCategoryId
select p;
If subCategoryId is not a single value (seems like that) then try to use Contains
productDetails = from p in db.Products
where subCategoryId.Contains(p.SubCategoryId)
select p;

How to change the decimal to 12 digit string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to change the double (currency) to 12 digit in c# toString();
For example
12.50 => 000000001250
Try this:
string value = ((decimal)(12.50 * 100)).ToString().PadLeft(12, '0');
int a = (int)(12.50 * 100);
Console.WriteLine((a).ToString("D12"));
Try this
string formattedValue = value.ToString("000000000000");
value being the input and formattedValue as the output.

Summation of data, linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to summarize the "count" field where "Room" and "object code" are equal.
Any ideas?
I have no idea how is your context set, what is the table name and dozen other things that are necessary to answer your question, but it will definitely use GroupBy LINQ method, something like:
var results = db.TableName.GroupBy(x => new { x.Room, x.ObjectCode })
.Select(g => new { g.Key.Room, g.Key.ObjectCode, Count = g.Sum(x => x.Code) })
.ToList();

Splitting a string into parts [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split a string in C# for example:
120530
so it will be like this:
Day: 12
Time: 0530
But it will be without spaces, just as it is 120530
How can I do that?
I'm assuming that you know that it will always be 6 digits with the first 2 being day and last 4 being time.
Utilize the Substring() method of your string object...
string allTogether = "120530";
string day = allTogether.Substring(0, 2);
string time = allTogether.Substring(2);
var dayAndTime = "120530";
var day = dayAndTime.Substring(0, 2);
var time = dayAndTime.Substring(2, 4);

Categories