|
There must be some mistake in your code prior to your orderby call, as you should be able to order by text. As a quick demo I did this
public partial class Form1 : Form
{
List<Person> sortedPeople;
public Form1()
{
InitializeComponent();
List<Person> people = new List<Person> {new Person("wayne"), new Person("sarah"), new Person("mark"), new Person("simon"), new Person("ashleigh"), new Person("dave"), new Person("connor"), new Person("bronwyn"), new Person("chantelle"), new Person("will"), new Person("chris")};
sortedPeople = people.OrderBy(name = name.Name).ToList();
}
}
class Person
{
public Person(string name)
{
this.Name = name;
}
public string Name { get; set; }
}
and SortedPeople came out correctly
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Hi,
try the below code snippet
var newList = people.OrderBy(name => name.Name).ToList();
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
Hi, thanks for looking at this.
Not sure what I am doing wrong.
I tried...
DataContext = new DataClasses1DataContext();
var people = from name in DataContext.Table_Peoples
where name.IQ < 3
select name;
var peopleOrdered = people.OrderBy(name => name.Name).ToList();
'Name' is decared as follows...
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="Text", UpdateCheck=UpdateCheck.Never)]
public string Name
{
get
{
return this._Name;
}
set
{
if ((this._Name != value))
{
this._Name = value;
}
}
}
It is still thowing the same exception.
|
|
|
|
|
Ok I got it to work like this...
DataContext = new DataClasses1DataContext();
var people = from name in DataContext.Table_Peoples
where name.IQ < 3
select name;
List<Table_People> listPeople = people.ToList();
var peopleOrdered = listPeople.OrderBy(name => name.Name).ToList();
But I dont understand why I have to copy to a List first?
|
|
|
|
|
Quote: __John_ wrote: List<Table_People> listPeople = people.ToList();
var peopleOrdered = listPeople.OrderBy(name => name.Name).ToList();
To order the list, it requires the set of names to be in a format and we are storing it as the enumerable list.
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
Hi Manognya,
I dont understand why List.OrderBy works when IEnumerable.OrderBy does not?
I am not sure what you mean when you say 'in a format'?
Thanks.
|
|
|
|
|
Hi John,
The syntax of OrderBy is:
Public Shared Function OrderBy(Of TSource, TKey) ( _
source As IEnumerable(Of TSource), _
keySelector As Func(Of TSource, TKey) _
) As IOrderedEnumerable(Of TSource)
Parameters
source
Type: System.Collections.Generic.IEnumerable(Of TSource)
A sequence of values to order.
keySelector
Type: System.Func(Of TSource, TKey)
A function to extract a key from an element.
we need to convert the source to be of type
System.Collections.Generic.IEnumerable
Hope this helps.
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
|
Thanks, it is slowly becoming clear, I probably just need to think about it a bit more.
I do sometimes find templates a bit confusing.
+5
|
|
|
|
|
Agree..templates is always little tricky(for me either )...But, Once got..makes life easy
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
The difference is not the IEnumerable or the List but where the OrderBy take place.
When you call ToList,or any greedy query operators, you execute your Ling query against your DB. After that, all Linq operation are executed in memory and Linq-to-object can do more things in than Linq-to-sql, or Linq-to-entities
Vince
Remember the dead, fight for the living
|
|
|
|
|
Thanks Vince, that explains it a bit more.
I think i can now understand what the debuger is showing me.
It uses a 'lazy' stratergy, ie. only evaluating an expression or executing a function when the result is actually needed, am I right?
BTW: How can I enumerate the results more that once?
Hoping that is not too stupid a question
Thanks - John.
|
|
|
|
|
|
__John_ wrote:
It uses a 'lazy' strategy, ie. only evaluating an expression or executing a function when the result is actually needed, am I right?
Yes you are right. It's a principle of Linq to defer execution until is needed. And Linq also evaluate only the elements needed to return the result
Let's take Wayne List and do some examples
List<Person> peoples = new List<Person> {new Person("wayne"), new Person("sarah"), new Person("mark"), new Person("simon"), new Person("ashleigh"), new Person("dave"), new Person("connor"), new Person("bronwyn"), new Person("chantelle"), new Person("will"), new Person("chris")};
int Count = people.Count(p => p.Name.StartsWith("w"));
var firstperson = people.Where(p => p.Name.Length == 5).Take(3);
foreach (var p in firstperson)
{
Console.WriteLine(p.Name);
}
var firstperson2 = people.Where(p => p.Name.Length == 5).Take(3);
people.Insert(1, new Person("Vince"));
foreach (var p in firstperson)
{
Console.WriteLine(p.Name);
}
http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx[^]
__John_ wrote: BTW: How can I enumerate the results more that once?
If you use a Enumerator<t>, you can use Reset to set the enumerator to its initial position, which is before the first element in the collection.
But if you use a foreach loop, you can reuse an Enumerable<t> many times. The foreach loop will start at the beginning every time.
Vince
Remember the dead, fight for the living
|
|
|
|
|
|
Nice examples - thanks!
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Nice examples.Thanks.Its more clear now.
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
Hello,
I have a VBA application and we are planning to convert that to Visual Studio 2010.
In the VBA application I am using a 3 D Array. But when I increase the size of the array it goes out of memory. Following is the code:
Global stockpileArray(-1 To 135, -1 To 80, -1 To 1090) As Block
Public Type Block
trainID As String
moisture As Double
blockType As BLOCK_TYPE
segmentNum As Integer
locationID As String
End Type
I would like to know the memory limitation of ASP.NET C# Application.
Can I create 30 of the above arrays in C# app?
Thanks,
|
|
|
|
|
|
The LED lights are on but it looks like one ones at home.
"You get that on the big jobs."
|
|
|
|
|
All .NET applications have a built in memory limitation: no single object may exceed 2Gb.
This means that an array of Strings may contain 268, 435, 456 strings (assuming the references are 64 bit).
Your array contains only 12,267,528 elements, but if these are composite objects which take up more than 175 bytes each then they will not fit in a single object.
You could try declaring Block as a Class instead of a Type - that should mean that each element of your array only takes a single reference instead of the whole Block. There will be a performance penalty associated with the extra access - how bad that would be will be down to how you use it.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Which certainly explains why applications you've got running on your 64-bit computer processor, applications which have proclaimed themselves 64-bit despite actually running under a Waring-blender-meld of bittedness, shimmed up by this and that in your own special way in order to run on your toaster, don't actually pop-up any toast that isn't burnt to a crisp.
(Late to the party I suppose ... but hey! There are icons for that now: )
|
|
|
|
|
Are you into reruns of "the best of OG" or something?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
At some point you've got to cast your aspersions upon something unidentifiable and today I was just exercising an old CP trick to see whether I could auspiciously peg the post that caused the ticker on the front page to make that sound ...
Guess I'm not lucky because it wasn't this umteenth page.
|
|
|
|
|
I agree with what OG said. I want to add you should reconsider using a 3D array. In a lot of situations, a multi-dimensional array is not really needed.
Also, do all elements contain meaningful data, or is a majority of them just empty? If a lot are empty, switching from a structure to a reference type is advantageous. And if all or most full, is there a major axis? you may want to consider jagged arrays (e.g. a 1D array of 2D arrays, each with smaller sizes)?
If you need more help, explaining what it is about would be a good start.
|
|
|
|