Browsing 7239 questions and answers with Jon Skeet

How to compare minutes portion only of a TimeSpan in LINQ to Entities?

I have a Publication entity in my model. I want to retrieve all publications that are created less than 10 minutes from now. var publications = myEntities.Publications. ...
Jon Skeet
people
quotationmark

Don't do the arithmetic in the query - do it before the query, so that you're basically specifying an "earliest publication creation time": // Deliberate use of UtcNow - you should almost certainly be storing UTC, not // local time... var... more 8/18/2014 2:27:15 PM

people

load foreign characters from xml file using StreamReader

Am loading an XML file but at the same time there are some Greek characters inside the file like "ναι" and when I load them on a data grid view table they appear like ���. Am...
Jon Skeet
people
quotationmark

The simplest answer here is not to use StreamReader at all. Let the XML parser handle the encoding appropriately: public XmlDocument LoadDocument(String x) { XmlDocument document = new XmlDocument(); using (var stream =... more 8/18/2014 12:51:33 PM

people

How to return a subset of XElements inside parent XElement

If I have xml such as the following: <ValidationResults> <ValidationResult> <Scheme>Scheme A</Scheme> </ValidationResult> ...
Jon Skeet
people
quotationmark

Your current approach seems the simplest one to me, although it could be done in a single statement as: var element = new XElement("ValidationResults", doc.Element("ValidationResults") .Elements("ValidationResult") ... more 8/18/2014 10:47:32 AM

people

Empty all XML nodes of a certain type using C#

I currently have a XML document built using the following structure: <a> .... <b> <c> </c> <d> ...
Jon Skeet
people
quotationmark

LINQ to XML would make this pretty simple: var doc = XDocument.Load(...); var bs = doc.Descendants("b").ToList(); foreach (var b in bs) { b.ReplaceNodes(); } (Use ReplaceAll instead of ReplaceNodes if you want to remove the... more 8/18/2014 10:07:42 AM

people

Create collection in Java without knowing the type?

For example, can I create List that will contain String and other List? And if I can do that, what is the specific of my List? I can just say that it contains data of type Object,...
Jon Skeet
people
quotationmark

Yes, you can use: List<Object> list = new ArrayList<>(); list.add("A string"); list.add(new ArrayList<Foo>()); // etc It's usually not a good idea, as when you get items out of the list, it can be a pain to work out... more 8/17/2014 7:35:54 PM

people

ERROR: The process cannot access the file 'Companion.jpg' because it is being used by another process

I have a problem when trying to delete the files from a directory because it says a file is being used by another process. I cant figure out how to delete all the files in the...
Jon Skeet
people
quotationmark

I suspect this is the problem: Image img = Image.FromFile(file.FullName); That opens the file - but you never dispose the same object, so the file will be open until the object is finalized. Image implements IDisposable, so you should... more 8/16/2014 9:35:36 PM

people

Writing a serialized object to file garbaged?

try { File dataFile = new File("C:/Users/keatit/Desktop/players.txt"); if(!dataFile.exists()) { ...
Jon Skeet
people
quotationmark

This is binary serialization. It's not meant to be writing to a human-readable text file. For that, you should look into something like JSON or YAML. I'd strongly recommend against writing to a .txt file using ObjectOutputStream - it gives... more 8/16/2014 9:18:50 PM

people

LINQ: Generate "AND" Expression instead of "OR" when using "CONTAINS"

I have this List: string[] countries = { "USA", "CANADA" }; When I run this query : query = (from user in db where user.Orders.Any(order =>...
Jon Skeet
people
quotationmark

So you want a list of the users such that all the countries in the list are present in the set of order destinations? Logically, that would be: query = from user in db where countries.All(c => user.Orders.Any(o =>... more 8/16/2014 9:09:27 PM

people

how to print double using rounded number of decimals

C# 4.0 ASP.NET MVC4 application contains Out method called from Razor views. It receives rounded double as parameter. protected static string Out( double roundedValue ) { ...
Jon Skeet
people
quotationmark

It's important to understand that float and double don't retain trailing zeroes. You have two options: Specify the format to include the number of decimal places you're interested in Use decimal, which does retain trailing zeroes It's... more 8/16/2014 8:52:59 PM

people

How to execute pl sql block using oledb in c#

I am executing Pl/Sql block using OLEDB in C#. The code is: string a1; a1 = discom1.Text.ToString(); my_querry10 = "BEGIN"+ "SELECT...
Jon Skeet
people
quotationmark

It seems to me that you don't need a separate block for this at all - you can just use SELECT COUNT(1) and ExecuteScalar: string sql = "SELECT COUNT(1) FROM MasterCompliant WHERE Discom = ?"; int count; // Open and close a connection... more 8/16/2014 8:21:48 AM

people