I had to think about your answer for a bit, you mean I should use something along the lines of
let myObj = ParseOrDefault(s)
where myObj != null
Yes, that works fine. Thank you again.
I had to think about your answer for a bit, you mean I should use something along the lines of
let myObj = ParseOrDefault(s)
where myObj != null
Yes, that works fine. Thank you again.
Much nicer, thank you!
I am parsing an XML with data I want to convert to objects of an already existing class (so I don't want to modify the class itself). There is a specific sub-element that each element may or may not have, and depending on whether that sub-element exists, I want to use a different object constructor in my select clause. This is how I did it (both code and xml are simplified):
<channel>
<display-name>SVT Europa</display-name>
</channel>
<channel>
<display-name>Eurosport</display-name>
<icon src="http://whatever" />
</channel>
var channels =
(
//Icon URL is not required so the LINQ is divided into two parts, first get channels with an icon URL.
from x in channelsXml.Root.Elements("channel")
where
x.Element("icon") != null &&
x.Element("icon").Attribute("src") != null &&
Uri.IsWellFormedUriString(x.Element("icon").Attribute("src").Value, UriKind.RelativeOrAbsolute)
select new Channel(
x.Element("display-name").Value,
new Uri(x.Element("icon").Attribute("src").Value)
)
)
//Secondly, add channels missing an icon URL.
.Concat(
from x in channelsXml.Root.Elements("channel")
where
x.Element("icon") == null ||
x.Element("icon").Attribute("src") == null
select new Channel(
x.Element("display-name").Value
)
);
Is this a good way to do it? My primary concern is whether the collection of "channel" elements is searched twice, and if there is a solution to the problem where it isn't. I also don't think it's very readable, hence the comments, is there a "cleaner" way to do it? (Remember that I simplified the code and xml, in the actual code several more elements are read, and a lot more error checking are handled in the where clauses, and a lot of the error checking is duplicated because of the .Concat) Comments? Insights? I'm new at LINQ so feel free to bash my way of using it if you like ;)
I am reading data from an XML using LINQ. Some elements contains data that I want to parse to a specific object type, say for example they are numbers that I want to parse to integers. If the data in one element is incorrect (i.e. "abc" where there should be a number) I just want to skip it and read the rest (meaning a try...catch around the entire LINQ would not work). If I were using loops I would just use a try...catch around the Parse method, with a continue;
in the catch. But how do I accomplish this using LINQ? One solution is to use TryParse in a where statement, but then I have to parse the text twice, first with TryParse in the where, and then with Parse in select. Ok, parsing an integer might be cheap, but say: *There is no TryParse for the object I want, only a Parse method throwing exceptions (ok, I could write a TryParse wrapper) or *The parsing is not cheap for the specific object type I want, and it would be a major performance hog having to do it twice. How would I go about doing this?
I have an application that runs in the background and pops up windows with reminders, thing is I don't want it to pop up the windows if I play a full screen game. How can I detect if a full screen application is running?
Thanks, but for some reason I can't get it to work. To be honest I didn't look into it that much since I managed to find another solution earlier. I modified your example to my actual project: DataRowView v = (DataRowView)source.Current; DateTime date = (DateTime)v.Row["Date"]; ...but it doesn't work. This works fine though: In class: //public version of GetColumnValueAtRow public object PublicGetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, System.Int32 rowNum) { return this.GetColumnValueAtRow(source, rowNum); } In Paint(...): DateTime date = (DateTime)((MyDataGridTextBoxColumn)this.DataGridTableStyle.GridColumnStyles["Date"]).PublicGetColumnValueAtRow(source, rowNum); -- modified at 12:46 Tuesday 17th January, 2006
I have found one solution for how to color a row in a DataGrid based on the data in the row. Here's one example: guffy.net[^]. The problem is that it doesn't work when the DataGrid is sorted, or when a RowFilter is applied. Example: I have a simple table with two columns, one with integer values and one with strings. 15 Some text -14 Different text 5 Another string 6 Yet another string Say I want all rows where the integer is negative to have a blue background. This is easily done using the example above. In this example the second row will be colored blue. But say I don't want to show any rows with an integer >= 10. When I create the DataGrid I use the code DataView mainDataView = ((DataTable)this.mainDataGrid.DataSource).DefaultView; mainDataView.RowFilter = "[Value] < 10"; Now the DataGrid will look like: -14 Different text 5 Another string 6 Yet another string But now the second row will have a blue background, instead of the first. How can I solve this problem? Any help is appriciated! (btw, I already asked this question in an old thread, but I'm new here and thought a thread would get "bumped" when it got a new message, turns out that the message bord is sorted by the first message in a thread, so I guess noone noticed that message...) /Cesa
Using this example I can set a row to a specific color based on its content, but it doesn't work if I sort the datagrid or use a DataView.RowFilter. Does anyone know how to make it work? To clarify, in the paint method I get the number of the row of the datatable, so if row #2 has a value that sets the background to red, row #2 in the datagrid becomes red. But if I use a RowFilter so that row #1 of the datatable isn't shown in the datagrid, it is still row #2 of the datagrid that is red even though the content has changed and the row that should be red now is row #2. /Cesa