|
Ok think i have solved the issue i was having, wit the lookupnot updating the control it was on, the cause was only scalars automatically trigger a change notification, the solution was adding the following to a partial class.
public partial class Charge
{
public Charge()
{
this.ActivityReference.AssociationChanged += new System.ComponentModel.CollectionChangeEventHandler(ActivityReference_AssociationChanged);
}
void ActivityReference_AssociationChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
{
if ((e.Action == System.ComponentModel.CollectionChangeAction.Remove))
OnPropertyChanging("Activity");
else
OnPropertyChanged("Activity");
}
}
If any one is able to share a best practice way to make the lookup use the same ObjectContext as the Window is using that would be most helpful. I think the line below outlines the situation i am in, and the aim is for the cEntities Context = new cEntities(); from the window to be used in the lookup when popluating the ComboBox.ItemsSource .
Window>Databound List View>Item Template>Lookup User Control
|
|
|
|
|
Hi,
I'm new to LINQ concept the requirement what i have is, I have a string column with data as below
OR0
OR1
OR2
.
.
OR9
.
.
OR21
ORD
Now i want to order it as above, but when i do order by i get data as below as it is ordering as string
OR0
OR1
OR10
OR11
OR12
.
.
OR19
OR20
OR21
OR3
.
.
OR9
ORD
Which is wrong as per my requirement.
I tried ordering as Order By Len(Col),Col and the o/p what i received is
OR0
.
.
OR9
ORD
OR10
.
.
OR21
Can any one help me with giving me a proper Order By statement for LINQ statement?
Regards,
Kishan
|
|
|
|
|
What you're looking for is known as a "Natural Sort," and there are plenty of implementations online. Unfortunately, as far as I'm aware, LINQ does not have this functionality built-in, so you have to code it yourself.
If you can translate a bit of C# code, this site[^] has a LINQ-based solution.
|
|
|
|
|
Thank you for your kind reply.
Can i know How to get the count of Alplabets in a Alphaneumeric column using LINQ
like
OR0 - 2
OR1 - 2
.
.
OR10 - 2
.
.
ORD - 3
If i can get this, i can achieve my requirement
Thanks in advance.
|
|
|
|
|
That won't be enough for a natural sort, unless you can guarantee that your input will always be letters (possibly) followed by numbers... What if one of your items was "OR10A"? Three letters, but should be before "OR2". What happens if you have something more complicated, with multiple sets of numbers in between letters?
But... If you're absolutely sure that your input will always follow this pattern, then you could take that shortcut by writing a quick function to count the number of letters... Something like this (Sorry if my syntax isn't perfect):
Public Function NumberOfLetters(s as String) as Integer
For idx As Integer = 0 To s.Length - 1
If Not Char.IsLetter(s(idx)) Then Return idx - 1
Next
Return s.Length
End Function
|
|
|
|
|
Hi Ian,
Very mcuh thanks for your kind reply.
Actually i can sort the way how i want with a Oracle condition as below
"order by to_number(decode(COL,'ORD','1000',substr(COL,3)));"
here i'm sure that My COL will be have only the values as OR1,OR2... OR21,ORD
I just want to Implement this in LINQ as below
"ORDER BY TONUMBER(Key1 = 'ORD' ? '1000' : p.ProductName.Substring(3))"
Can you please help me to correct the above syntax as i'm sure its a wrong one.
Regards,
Kishan
|
|
|
|
|
You really need to get away from this fascination with LINQ... It's not the solution to all of your problems. In this case, you can't use the LINQ query syntax... It only works with simple properties.
But once you get the data into a collection, you can sort it via:
.OrderBy(Function(key) key == "ORD" ? 1000 : int.Parse(key.SubString(3))
|
|
|
|
|
|
Don't post here *and* in the Questions/Answer section.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Hi,
I want to restrict users that are logged on a computer from a guest account from running my .NET C# application. What is the recommended way to do this?
Best Regards
Olof
|
|
|
|
|
You've already asked this in C# forum. Please, do not post the same question in several forums.
|
|
|
|
|
sorry erik, i forgot to remove this post
modified on Tuesday, January 18, 2011 5:07 AM
|
|
|
|
|
Please go through this link:
This[^]
|
|
|
|
|
Hi,
I would like to get a few expert opinions on the question of when to define a custom exception.
According to some books I have read, the exceptions built into .NET should generally be reserved for .NET's own use and developers should create custom exceptions for classes they create. This seems to me to be unnecessary - and even undesirable - at times. For example, if I had a class with a function that had a single integer parameter that should always be in the range 0-9 and it receives 42, I would have thought the best exception to throw would be the predefined System.ArgumentOutOfRangeException; it seems to fit the bill perfectly.
If you were creating a whole code library, I can see the value of defining a custom set of exceptions for the library, but for individual lumps of code, it seems like overkill.
I would be very interested to hear other opinions on this subject or get links to good articles on this subject.
Best wishes, Patrick
|
|
|
|
|
Hmmm. I've never read anything to suggest that you shouldn't be able to use the standard exceptions. It's insane for somebody to suggest otherwise because they are effectively telling you that your code cannot act like the underlying framework. What do I mean by this?
Well, suppose that you have written a framework of your own that sits on top of the .NET framework (an MVVM framework for instance), and it gains popularity with other developers. Now, this framework checks parameters to ensure they aren't null - it's only natural for the end developer to expect that a null parameter would be raised via an ArgumentNullException - after all, he's making other calls that are throwing this exception.
Suppose now, that you've used some GPL in your library, and in this code, it also checks for null arguments and in certain places, your code is such a thin wrapper that you just pass the arguments that you've got across. All of a sudden, you've got two different types of custom ArgumentNullException to handle, and that's just plain stupid.
If you have an exception type that isn't catered for by the underlying framework then, by all means, create your own exception for it - I do this quite a lot (for instance, I have an UnexpectedStringFormatException exception that I sometimes raise when I have a regular expression test that fails). If it's something as simple as an argument out of range, then use the ArgumentOutOfRangeException because that's what it's there for.
|
|
|
|
|
The authors that you're referring to have heard about the rule-of-thumb that you should inherit your own exceptions, preferably not inheriting from ApplicationException . The reasoning is that it helps in differentiating between the exceptions that the framework throws and the exceptions that you app throws.
Not considering the fact that most shops create their own framework on top of .NET, and have multiple apps on that framework. According to their rule-of-thumb, the apps shouldn't re-use the exceptions from their own framework.
..now, if programming could be reduced to a few rule-of-thumbs, we'd all be using Visual Basic. By all means, throw an OutOfRangeException if that's what's occuring - the goal of an exception is handling an exceptional situation, and if OutOfRange describes that best, then that is the one that should be applied.
I are Troll
|
|
|
|
|
I like to create my own exceptions, except (no pun intended) for the most basic validations, because the situation you mentioned is a bit rare in business applications. The custom exception often adds semantic information. In a similar situation, NegativeDepositAmmountException IMHO could be much more meaningful than ArgumentOutOfRangeException. If your number has a strict domain, often is a class or some entity that needs special treatment.
Another similar case is where you can't receive a Customer with an empty or null Customer.Address field. What would you prefer to receive? A NullReferenceException or a EmptyAdressException?
It means a lot more manual work, but in the long run it pays when you deploy your code to your customers.
|
|
|
|
|
If you have NegativeDepositAmmountException inherit ArgumentOutOfRangeException you can have the best of both worlds. You can encode as much semantic information as you need, while simultaneously making it easy for a consumer to handle common error cases without having to learn all the details of your implementation up front.
3x12=36
2x12=24
1x12=12
0x12=18
|
|
|
|
|
It's a very nice idea, but not always practical, and could lead to some inconsistencies. I learned the hard way to take a lot of care when deriving a class, and follow more rigidly LSP (Liskov Substitution Principle).
In the same example, EmptyAdressException cannot be derived from NullReferenceException, in the same way a Square cannot be derived from a Rectangle.
|
|
|
|
|
I'm not following. If I understand LSP correctly it says that if I derive Square from Rectangle that Squares must have all the properties of a rectangle; which they do so the derivation should be allowed under LSP.
What am I missing?
3x12=36
2x12=24
1x12=12
0x12=18
|
|
|
|
|
That's what I thought at first, too. But LSP is not about properties, nor methods. LSP is about "substitution", and that is more closely related to an specialization. A Red Rectangle can be derived from a Rectangle, but a Square is much more an abstract class on its own.
The "is a" relationship of OO is flawed; you can read more about that on this pdf[^]
|
|
|
|
|
Also, a inherited class should only throw exceptions that the base class throws, or descendants thereof.
I are Troll
|
|
|
|
|
IMO, it really depends on what description the pre-defined .NET exception is offering. Is it enough for the user to understand what went wrong? If it isn't, then you should create your own exception and put in a custom name and message to let the user know exactly about the problem.
|
|
|
|
|
I would say you need to build your own exception set when you (your clients) don't like the exception set Microsoft is providing - as simple as that.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
If the condition that triggers the exception is reasonably generic (a null argument, or a value out of range), I use one of the predefined .NET exceptions. If the condition is domain-specific to the application, then I define my own.
For example, I have a class that handles TCP/IP socket communications between my application and a Windows service. This class can throw InvalidCastException s, given that our protocol includes data type information along with each data value in a message. This ensures that the sender/receiver on my end agrees with the receiver/sender on the other end. It also can throw BufferException or ReadSizeException , exceptions defined in the class derived from ApplicationException , that reflect protocol errors.
Patrick Skelton wrote: According to some books I have read, the exceptions built into .NET should generally be reserved for .NET's own use and developers should create custom exceptions for classes they create.
This is symptomatic of an 'anti-pattern' you see occasionally. Some programmers think that they always have to insulate their applications from potential changes to the framework they're using. They build wrapper classes for everything. The wrappers end up being extremely thin layers on top of the actual framework. All they accomplish with this is to obfuscate their use of the framework. This makes their code more difficult to debug and maintain, since they rarely document framework considerations and constraints that their wrapper promotes.
Software Zen: delete this;
|
|
|
|