Browsing 7239 questions and answers with Jon Skeet

Best sequence using LINQ

I have ordered list like in example var someList = new List<int>{1,1,2,3,5,2,1,3,7,1}; I want to select by using LINQ best(highest sum) sequence of 3 numbers. In this...
Jon Skeet
people
quotationmark

You can use Skip/Zip to end up with triples. For example: var triples = list.Zip(list.Skip(1).Zip(list.Skip(2), (b, c) => new { b, c }), (a, bc) => new { a, bc.b, bc.c }); (That may have some errors - I... more 1/28/2014 5:36:09 PM

people

Converting a SQL Query to a Predicate Expression using Fluent Synatx

How do I write the following sql query to a Predicate Expression using Fluent Sytax: select c.* from customer c inner join Orders r on r.CustomerId = c.Id where c.MemberSince...
Jon Skeet
people
quotationmark

You don't need PredicateBuilder at all. You just need to understand that the result of a join is effectively a sequence of pairs: DateTime memberCutoff = ...; DateTime orderCutoff = ...; var query = context.Customers ... more 1/28/2014 5:31:43 PM

people

Compact Framework C# loading DLL dynamically

instead of including a dll file in my project as a reference, I need to dynamically use some classes from specific namespaces in that dll file. I've done some research about the...
Jon Skeet
people
quotationmark

Invoking the member is done as normal - but you can then cast the result to IList: IList array = (IList) type.InvokeMember(...); object firstElement = array[0]; (All arrays implement IList, which is slightly simper to use in this case... more 1/28/2014 4:53:23 PM

people

Is Code of methods for different object of same class present in executable?

If I create an array of 10 objects of class A, will the object methods be present (in the assembly) 10 times for each object or just single instance of the method code which will...
Jon Skeet
people
quotationmark

No, the code belongs to the class, and will only be represented once. The array and the objects don't even exist until execution time - given that the size of the array could vary, the executable file couldn't possibly contain the code... more 1/28/2014 4:42:34 PM

people

Java: Trouble creating an array from file

Hey I have this code here: public class Levels { boolean newGame = true; public void newGame() { while (newGame) { int cLevel = 1; List<String> list =...
Jon Skeet
people
quotationmark

This is the problem: String cLine = bf.readLine(); while (cLine != null) { list.add(cLine); } You're not reading the next line in your loop (the value of cLine never changes) - so it's just going to loop forever. You want: String... more 1/28/2014 2:53:04 PM

people

Is my usage of async/await correct here?

I'm writing an HTTP server. There are many like it, but this one is mine. It contains the following code: private async void Listen(object state) { while...
Jon Skeet
people
quotationmark

Well, firstly I'd use Task.Run instead of Task.Factory.StartNew, just for simplicity :) The next thing is to consider holding onto these tasks not to await them immediately, but so that you can know when they've all finished in order to... more 1/28/2014 12:33:55 AM

people

C#.Net Threads are grabbing the same files within a loop

I'm trying to design a program that uses an external OCR application to flip an image until its right side up. All of the image locations are kept in files[]. The problem is,...
Jon Skeet
people
quotationmark

The problem is that your lambda expression is capturing the variable i, rather than its value for that iteration of the loop. There are two options: Capture a copy for (int i = 0; i < files.Length; i++) { int copy = i; ... more 1/28/2014 12:04:37 AM

people

How does inheritance of instance fields work in this particular code?

class A { int a = 2, b = 3; public void display() { int c = a + b; System.out.println(c); } } class B extends A { int a = 5, b = 6; } class...
Jon Skeet
people
quotationmark

why does the output comes 5,5? Because A.display() only knows about the fields A.a and A.b. Those are the only fields that any code in A knows about. It looks like you expect the declarations in B to "override" the existing field... more 1/27/2014 10:08:43 AM

people

Async Restsharp getting data into datacontext

public class BerichtenOphalen { public List<Bericht> berichten = new List<Bericht>(); public List<Bericht> getBerichten(string email) { var...
Jon Skeet
people
quotationmark

Your code is currently broken - you've got two variables called berichten, one of which is a local variable within the method, and one of which is an instance variable in the class. You're returning the value of the local variable, but... more 1/27/2014 8:16:56 AM

people

java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.ArrayList.toArray(Unknown Source) at Hai.main(Hai.java:20)

I am getting the following error when executing the code below... I am trying to convert the list of Object arrays to a BigDecimal array using the toArray() method of the List...
Jon Skeet
people
quotationmark

The problem is that the element in fileFormatObj is an Object[], not a BigDecimal. Therefore you can't convert fileFormatObj to a BigDecimal[]. You can't even convert it to a BigDecimal[][] (as an Object[] isn't a BigDecimal[]) but at the... more 1/27/2014 7:35:14 AM

people