Browsing 7239 questions and answers with Jon Skeet

Full outer join linq using union

I have two lists var left={[ID=1,Name='A',Qty1=0,Qty2=5],[ID=2,Name=B,Qty1=0,Qty2=52]}; var right={[ID=1,Name='A',Qty1=57,Qty2=0],[ID=2,Name=B,Qty1=84,Qty2=0]}; var...
Jon Skeet
people
quotationmark

It sounds like you possibly want an inner join: var query = left.Join(right, l => l.Id, r => r.Id, (l, r) => new { l.Id, l.Name, r.Qty1, l.Qty2 }); (You may want to join on both Id and Name; it's not clear... more 2/1/2015 9:07:11 AM

people

Does { } act like ( ) when creating a new object in C#?

I just noticed that using {} instead of () gives the same results when constructing an object. class Customer { public string name; public string ID {get; set;} } static...
Jon Skeet
people
quotationmark

There are three ways of directly creating a new object in C#: A simple constructor call with an argument list: new Foo() // Empty argument list new Foo(10, 20) // Passing arguments An object initializer with an argument list new... more 1/31/2015 5:40:28 PM

people

Parallel.For not to use my main thread

In my application I want my main thread to be not used by anything else. I have to do some parallel processing that I would like to be done by different threads. For that I am...
Jon Skeet
people
quotationmark

Is there some way to make sure that anything in Parallel.For always run on separate thread so that main thread is always free. Parallel.For will always block until everything is finished - so even if it didn't do anything on the... more 1/31/2015 3:20:49 PM

people

Linq query sum up the amount column

var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 ...
Jon Skeet
people
quotationmark

Firstly, you're using SingleOrDefault which will only select a single value. You want to use Sum instead. Also, you don't need an anonymous type here. You can just use: var result = (from t1 in _dbEntities.Charges join t2 in... more 1/31/2015 2:45:18 PM

people

Add postfix in java 8 way

I need a function which returns a set containing {XXX11, XXX12, XXX13, XXX14, XXX21, ... , XXX43, XXX44} where XXX is a integer argument of the function. What is a elegant way of...
Jon Skeet
people
quotationmark

The simplest way is probably to just generate 16 values: Set<Integer> addOnlyTwoPrefixes(int base) { return IntStream.range(0, 16) .map(prefix -> base * 100 + // Leading digits 10 + 10 *... more 1/31/2015 12:13:38 PM

people

Using LINQ expression to get previous element from a collection

I need to write a LINQ expression based on the following scenario: public class Meeting { public int Id { get; set; } public DateTime Date { get; set; } public bool...
Jon Skeet
people
quotationmark

You could use TakeWhile and LastOrDefault: var meeting = Meetings.TakeWhile(m => !m.Selected) .LastOrDefault(); if (meeting != null) { // Use the meeting } else { // The first meeting was selected, or... more 1/30/2015 2:57:53 PM

people

java string index out of bound exception

It seems like a simple question but I'm wondering why if I had some String variable like this: String name = "John"; And then I'm using the substring method like this...
Jon Skeet
people
quotationmark

The second parameter to substring is an exclusive upper bound - so it's allowed to be equal to the length of the string, in order to include the last character. Likewise it makes sense to allow the starting point to be "at" the end of the... more 1/30/2015 2:26:37 PM

people

Compare Object superclass package with Object package

I'm trying to check if an object has a superclass in the same package. I made the following example Vehicle.java package myPackage; public class Vehicle { public...
Jon Skeet
people
quotationmark

You're calling c.getClass().getPackage() when you should be calling c.getPackage(). c is already the superclass - it's a Class, so calling getClass() on it will just give you Class.class, which isn't what you want. I would try to be more... more 1/30/2015 10:43:54 AM

people

Convert byte[] with binary data to String

I have data in binary format (hex: 80 3b c8 87 0a 89) and I need to convert that into String in order to save binary data in MS Access db via Jackcess. I know, that I'm not...
Jon Skeet
people
quotationmark

In order to safely convert arbitrary binary data into text, you should use something like hex or base64. Encodings such as UTF-8 are meant to encode arbitrary text data as bytes, not to encode arbitrary binary data as text. It's a... more 1/30/2015 10:29:09 AM

people

NodaTime: Time zone related issue using NodaTime library c#

here i am giving my code and what happen. when i am passing timezone id to .net time zone that works the code as below var zoneId = "India Standard Time"; var zone =...
Jon Skeet
people
quotationmark

If you want to pass a BCL time zone to Noda Time, you just need to use the BCL provider: DateTimeZone _zone = DateTimeZoneProviders.Bcl[zoneId]; This will find the relevant TimeZoneInfo, extract its adjustment rules, and convert that... more 1/30/2015 6:58:56 AM

people