|
DaveyM69,
Since your using a List<t> why not do:
return list[list.IndexOf(ID)];
And you could always add list.Containts(ID) before the above to check its in the list so you dont get an exception. It just seems simpler/quicker than iterating.
Regards,
Gareth.
|
|
|
|
|
I would write it this way:
public string GetName(int ID)
{
return
(from dvd in this
where dvd.ID == ID
select dvd.Name
).FirstOrDefault() ?? "";
}
If performance of GetName is an issue, neither a loop nor LINQ is efficient; you should use a Dictionary ID->dvd.
|
|
|
|
|
Daniel Grunwald wrote: FirstOrDefault()
I was just looking at the Element Operations on MSDN
I've tried various ways of coding it and Linq is definately slower in ALL situations (it is more 'elegant' though) by between 30 and 50%.
Performance isn't really an issue in this app but I didn't want to deliberately add overhead.
Thanks for all your help and suggestions everyone!
Dave
|
|
|
|
|
One of the reasons that you would use LINQ is that it allows you to write simpler code.
You query for the DVD could be a lot shorter.
public string GetName(int ID)
{
return this.Where(d => d.ID == id).Select(d => d.Name).FirstOrDefault() ?? String.Empty;
}
This is acually quite efficient, as the linq operator doesn't copy any data, but uses chained yield return methods to find the required subset of dvd's. So Link is actually just as efficient as your code would be, with the added advantage of a simple query language.
WM.
What about weapons of mass-construction?
"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
My blog
|
|
|
|
|
I've just tested it on one million records searching for the id 999999 and it still performs about 30 to 50% slower than a foreach loop.
It seems that regardless of what you atually try to do with a Linq query it calls some sort of internal ToArray() method (so the result supports the IEnumerable interface I assume) and that's where the performance is lost.
For complex queries I think Linq has some huge advantages, but for simple ones it seems the old foreach is better.
Dave
|
|
|
|
|
Why not simpy:
public string GetName(int ID) {
DVD found = this.Find(d => d.ID == ID);
return found != null ? found.Name : string.Empty;
}
But, as Daniel said, if performance is an issue, you should use a dictionary instead of a list.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
The dictionary would be fine if a 'real world' class were actually as simple as the example. If matching multiple properties then it's not much use.
That code performs 10 to 20% quicker than a foreach! Cool, thankyou!
Dave
|
|
|
|
|
DaveyM69 wrote: f matching multiple properties then it's not much use.
Then just make a key that contains multiple properties. Make a class that implements IEqualityComparer and use that as comparer when you create the dictionary.
DaveyM69 wrote: That code performs 10 to 20% quicker than a foreach!
Hm... that's pretty strange, as all the Find method does is looping through the items and calling the delegate...
A dictionary would perform about 100000% faster...
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
DaveyM69 wrote: If matching multiple properties then it's not much use.
Regarding that, I wrote an article about how to make a custom key for a dictionary:
Dictionary with a custom key[^]
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi,Please tel me how I can scan a document in c#.thanks.
Chamanara Shapoor
|
|
|
|
|
Member 2671314,
What do you mean by "scan a document"?
Regards,
Gareth.
|
|
|
|
|
I would assume you want to loop through the text of the entire document for single terms/phrases. Just use a for loop or while(!stream.endoffile).
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
Buy an OCR toolkit and hook it in, or look at writing a TWAIN class. There should be some samples floating around that you could work from.
|
|
|
|
|
Hi,
thanks.twain?where i can find this?
Chamanara Shapoor
|
|
|
|
|
I want to convert an RGB Image to YCrCb Image. when i get the values for Y,Cr and Cb for every pixel Using Its RGB Values... what am i supposed to do with these values to get The Resultant Image ?
(Jameel)
|
|
|
|
|
i want help,because my program is not working properly.If anyone can help me that would be a great help for me.The codings which I tried is given below.
How to print a pyramid?
/using System;
class pyr
{
static void Main(string[] args)
{
int j,k,m;
int n;
Console.WriteLine("No of lines:");
n = Convert.ToInt32(Console.ReadLine());
for (j = 0; j <= n; j++)
{
for (k = 1; k <= n- j; k++)
{
Console.WriteLine("*");
}
for (m = 1; m <= j; m++)
{
Console.WriteLine("*");
}
for (m = 1; m < j; m++)
{
Console.WriteLine("*");
}
}
}
}/
Thanks.
|
|
|
|
|
Why isn't it working properly?
Why do you want to print a pyramid?
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
for example i want to print a pyramid like this.
*
***
*****
*******
But it is printing like this
*
*
*
*
*
|
|
|
|
|
Do you know about new line ? ( Like /n in C/C++). If yes then use it.
Regards.
|
|
|
|
|
WriteLine includes a linefeed. You probably want to use just Write
|
|
|
|
|
Part of the reason why your program is not working is that you for statements are not proper or for that matter syntactically correct. Their are only 3 arguments in the for loop, you have extra semi-colons all over your for loops. Along with that I cannot see a correct conditional statement in the for loop (the middle paramter).
Try something along these terms
Console.Write("Enter # of lines:");
int temp=Convert.ToInt32(Console.ReadLine());
for(i=0;i<temp;i++)>
{
for(int j=0;j<=i;j++)
{
Console.Write("*");
}
Console.WriteLine();
}
I did not test this code, but it should work. Since this is your homework assignment you will have to figure out how to line up the asterisks to make a nice pyramid. Right now this should produce a right triangle.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
Hi,
I have 2 problems with crystal report:
1- when my DB (SQL Server 2005) have userLogin, in each page that a report should be displayed a login form appears for DB user.
2- when my server (connection string) changes, there is no filxed place to change the servername and the reports that is biuld on a server with name 'A' don't work on server 'B', and should be rebuild in new server.
a place such as Settings can not be used for connectionstring??
Best wishes
|
|
|
|
|
hi,
what is the solution of this type error
public class updates<br />
{<br />
public static int b1(int bb)<br />
{<br />
if (ViewState["str3"] == null)<br />
{ bb = 0; }<br />
else<br />
{ <br />
bb = (int)ViewState["str3"];<br />
<br />
<br />
}<br />
bb--;<br />
ViewState["str2"] = bb;<br />
<br />
return bb;<br />
}<br />
}<br />
<br />
protected void Button6_Click(object sender, EventArgs e)<br />
{<br />
l= updates.b1(l);<br />
TextBox3.Text = "" + l;<br />
}
Error 1 An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Control.ViewState.get'
|
|
|
|
|
zeeShan anSari wrote: Error 1 An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Control.ViewState.get'
Your class has no concept of what ViewState is.
You need to obtain a reference to the class that has the ViewState property and refer to it from there.
If this is the currently executing page, use (Page)HttpContext.Current.CurrentHandler .
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
i am using a combobox and a textbox control. text box behaviour depends upon selected value of combobox. These properties of combo box are assingned.
AutoComplete = true;
AutoCompleteMode = SuggestAppend;
AutoCompleteSource = CustomSource
AutoDropDown = true;
Further more .net 2.0 is under usage with visual Studios 2005.
in combobox_TextChanged() event i have assigned some value to textbox control. now i want that when user is typing over the combo box then as he types first letter the TextChanged() event is triggered and my textbox values changes accordingly. i want to control it some how so that when user ends typing only then my textbox values are changed. I tried to control it through combobox.selectionStart and combobox.selectionLength properties but thay always give 0 values. So how should achieve my goal??
Furthermore when user start typing over combobox and in case selection changes then no key event is trigered (i.e keyPress(), keyUp() or keyDown() events)but it selection doesn't changes only then keyUP() event is trigered. Moreover event of combobox_SelectionChangeCommitted()is also tested and it is not triggered when user types on combobox.
Any suggestions?
May God help u solv ur problems as well,thx in advance!!
|
|
|
|