Browsing 7239 questions and answers with Jon Skeet

Linq Sum with group by

I am having a table structure with columns FeesNormal FeesCustom Currency Now i am looking for a SUM function group by currency . For example 20 USD + 30 EURO + 40 INR ...
Jon Skeet
people
quotationmark

It seems to me that you just need a projection from "entry" to "effective fee" which you can sum - something like: var result = source .GroupBy(x => x.Currency) .Select(g => new { Currency = g.Key, Total =... more 5/20/2016 6:00:47 AM

people

why is my `getClass().getResource("logs")` returns null?

I have this code: this.fileName = getClass().getResource("logs").toURI().toString(); which fails for null pointer exception because getClass().getResource("logs") returns...
Jon Skeet
people
quotationmark

You're looking for a resource which is relative to the LogUtils class. You could use getClass().getResource("/logs") or you could use getClass().getClassLoader().getResource("logs"). (In either case, I'm not 100% convinced it'll work... more 5/19/2016 10:45:17 AM

people

How to Select a grandchild element of a element using xDocument and Linq

I have this XML markup: <preferences> <section name="PREF_SECTION_NAME_1"> <preference name="PREF_EXAMPLE_1" type="text"> <default...
Jon Skeet
people
quotationmark

Well yes, you're asking for the attributes of preference. It sounds like you actually want something like: var defaults = from pref in xDocUser.Descendants("preference") where (string) pref.Attribute("name") ==... more 5/19/2016 10:32:25 AM

people

What requirements on type used in IEnumerable.GroupBy()?

I was using GroupBy with an anonymous type: var v = list.GroupBy(x => new {x.street, x.houseNumber}); var o = v.Single(x => x.Key.street == "My Street" &&...
Jon Skeet
people
quotationmark

What do I need to add to StreetAddress so this works as it did with an anonymous type? Override Equals and GetHashCode, basically. Ideally, implement IEquatable<StreetAddress>, but mostly for the sake of clarity. Your query... more 5/18/2016 11:47:24 AM

people

Is this line of code from ILSpy decompiler valid?

I decompiled a release assembly using ILSPy and I got code like below. When I open the decompiled project in VS 2013, I get an error for each of these statements. using #j;//this...
Jon Skeet
people
quotationmark

No, it's not valid C#. Chances are it's decompiled code that was obfuscated to start with, so using identifiers that are valid in IL but not in C#. Typically, if you're decompiling obfuscated code, you're doing something against the... more 5/17/2016 7:53:36 PM

people

Combine a character constant and a string literal to create another constant

I code in C# primarily these days, but I coded for years in VB.NET. In VB, I could combine a character constant and a string literal to create other constants, which is very...
Jon Skeet
people
quotationmark

Is there a reason I can't do this in C#? Yes, but you're not going to like it. The string concatenation involved in char + string involves implicitly calling ToString() on the char. That's not one of the things you can do in a... more 5/17/2016 7:10:44 PM

people

Will be an object garbage collected if there is a reference to it from another object?

It is not a duplicate, I would like to know about this in context of C#. I have such classes: public class A { public List<string> AList { get; set; } } public class...
Jon Skeet
people
quotationmark

Yes, the variable that objectA refers to will be - or at least may be garbage collected. There's nothing referring to it any more. (There's no guarantee that it will collected "soon", but it's eligible for garbage collection.) The list... more 5/17/2016 4:13:16 PM

people

XML.Linq get value based on another value

I am trying to user Linq to XML to pull out a value from some xml based on another value.. Here is my xml <Lead> <ID>1189226</ID> <Client> ...
Jon Skeet
people
quotationmark

You're looking for the wrong thing at the moment. You should be looking for a CustomField element with a Name element with the specified value: string target = "Scotsm_A_n_Authority_Good"; // Or whatever var xmlAnswers =... more 5/17/2016 9:13:01 AM

people

Why is there difference in type intellisense/type inference for certain GUI controls when filtering Events

I observed that based on the type of control VS will sometimes correctly identify the type it belongs to, but sometimes it will just stick to a somewhat general EventArgs. Is...
Jon Skeet
people
quotationmark

Is this some winforms/.Net artifact? Well it's not particularly specific to Windows Forms, in that you could see it anywhere... it just depends on which delegate type was used to declare the event. For example, TabControl.Selected is... more 5/16/2016 6:25:21 AM

people

Comparing the value of a variable on json in C# to a string

I'm using a google API to get distance between two places and to check if I got distance information there's a status field on the json response for the API that can read "OK" or...
Jon Skeet
people
quotationmark

I suspect the problem is that the compile-time type of disJSON["rows"][0]["elements"][0]["status"] is JToken or something like that. If you cast to string, then a) you'll be comparing a string with a string; b) the compiler will use the... more 5/15/2016 8:25:30 PM

people