LINQ: throwing exception when Null is returned to a string? Attribute on a class?

As a string being null isn't particularly exceptional, you could do something like: var items = myStrings. Where(s =>!string. IsNullOrEmpty(s)).

Select(s => new Item(s)) UPDATE If you are reading this data from an XML file, then you should look into LINQ to XML and also use XSD to validate the XML file rather than throwing exceptions on elements or attributes that don't contain strings.

As a string being null isn't particularly exceptional, you could do something like: var items = myStrings. Where(s =>!string. IsNullOrEmpty(s)).

Select(s => new Item(s)); UPDATE If you are reading this data from an XML file, then you should look into LINQ to XML, and also use XSD to validate the XML file rather than throwing exceptions on elements or attributes that don't contain strings.

Yes! You are write! So I use the required element on XML like so .

– Martin Mar 28 at 22:22 problem with linq to xml you have to use magic strings! Msdn.microsoft. Com/en-us/library/bb387061%28VS.90%29.

Aspx thats why I created a class from my XSD to I can use strongy typed code. – Martin Mar 28 at 22:24.

You could try intentionally generating a NullReferenceException: try { //Doesn't change the output, but throws if that string is null. MyStrings. Select(s=>s.ToString()); } catch(NullReferenceException ex) { ... } You could also create an extension method you could tack on to a String that would throw if null: public static void ThrowIfNull(this string s, Exception ex) { if(s == null) throw ex; } ... myString.

ThrowIfNull(new NullReferenceException()).

This sounds like throwing the baby out with the bath water for something that should not happen in the first place. If you just want to detect that there are null/empty items: int nullCount= items. Count( x=> string.

IsNullOrEmpty(x. Name)); If you want to filter them out: var localText = from t in items where!string. IsNullOrEmpty(t.

Name) select new Items { item = t. Name }.

Count also has predicate argument – Snowbear Mar 28 at 22:00 @Snowbear - thanks, fixed - that simplifies that statement – BrokenGlass Mar 28 at 22:02 I want to throw an exception because there are coming from an xml file which I import into a class and use linq2objects on the class.. But this is a string, but my rules say that is can't be null or empty. I can't control this as its an xml file and I must validate it – Martin Mar 28 at 22:05 1 @Martin: I would make validation a separate step before mapping the data to your class - if validation succeeds do the mapping otherwise log a failure or do whatever else necessary, but I wouldn't mix this with your data mapping - at that stage you should have valid data to work with. Again, this is only my opinion - that's how I would approach it.

– BrokenGlass Mar 28 at 22:10.

I have a linq query which I am selecting into a string, of course a string can contain null! So if there a way I can throw an exception within my linq query, if I detect a null? Can I decorate my class with an attribute that won't let it allow null?

I would like to wrap my linq query in a try catch.. and as soon as a null is detected then it would enter the catch.. and I can handle it. Basically item is set to t.name, t.name is a string so it could be empty / null is this perfectly legal as its a string and strings can hold NULL. So if it returns NULL then I need to throw an exception.

Actually it would be handy to be able to throw an exception is NULL or empty. I seemed to remember some kind of Attributes that can be set on top of properties that Says "Don't accept null" etc..? Doesn't allow null or strings so I presume it throws an exception, I have used this with MVC but not sure if I can use it with a standard class? ANyone confirm this?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions