By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Linq. Select

P: n/a
Hello,

I have the following LINQ:

List<Tagtags = (from t in
database.Tags
select t).ToList();

I need to restrict my results and for that I have two parameters:
StartWidth and N
I would like to change my Linq query to do as follows:

1. Select all tags which tag.Name starts with the value contained in
StartWidth parameter.
NOTE: Both tag.Name and StartWidth are strings.

AND

2. Get the first N tags from that list ordered alphabetically.
NOTE: If N = 0 then get all tags

How can I do this?

Thanks,
Miguel
Jul 11 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
shapper wrote:
I have the following LINQ:

List<Tagtags = (from t in
database.Tags
select t).ToList();

I need to restrict my results and for that I have two parameters:
StartWidth and N
I would like to change my Linq query to do as follows:

1. Select all tags which tag.Name starts with the value contained in
StartWidth parameter.
StartWidth or StartsWith? The latter would make more sense.
NOTE: Both tag.Name and StartWidth are strings.

AND

2. Get the first N tags from that list ordered alphabetically.
Alphabetically by what? I assume "Name".
NOTE: If N = 0 then get all tags

How can I do this?
Well, just write it down in steps. This is all tags:

from t in database.Tags
select t

Now we want all tags where tag.name starts with "StartsWith" (I assume). So:

from tag in database.Tags
where tag.Name.StartsWith(StartsWith)
select tag

We want them ordered by Name:

from tag in database.Tags
where tag.Name.StartsWith(StartsWith)
orderby tag.Name
select tag

This is the sequence we want. Now we want to limit the number of elements,
for which LINQ offers the extension method "Take":

var q =
from tag in database.Tags
where tag.Name.StartsWith(StartsWith)
orderby tag.Name
select tag;

List<Tagtags = (N 0 ? q.Take(N) : q).ToList();

And that's all there is to it. See also
http://msdn.microsoft.com/vcsharp/aa336746 for examples of LINQ in action.

--
J.
Jul 11 '08 #2

P: n/a
On Jul 11, 6:11*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
shapper wrote:
I have the following LINQ:
* * * List<Tagtags = (from t in
database.Tags
* * * * * * * * * * * * * * * * *select t).ToList();
I need to restrict my results and for that I have two parameters:
StartWidth and N
I would like to change my Linq query to do as follows:
1. Select all tags which tag.Name starts with the value contained in
StartWidth parameter.

StartWidth or StartsWith? The latter would make more sense.
* * NOTE: Both tag.Name and StartWidth are strings.
AND
2. Get the first N tags from that list ordered alphabetically.

Alphabetically by what? I assume "Name".
* * NOTE: If N = 0 then get all tags
How can I do this?

Well, just write it down in steps. This is all tags:

* *from t in database.Tags
* *select t

Now we want all tags where tag.name starts with "StartsWith" (I assume). So:

* *from tag in database.Tags
* *where tag.Name.StartsWith(StartsWith)
* *select tag

We want them ordered by Name:

* *from tag in database.Tags
* *where tag.Name.StartsWith(StartsWith)
* *orderby tag.Name
* *select tag

This is the sequence we want. Now we want to limit the number of elements,
for which LINQ offers the extension method "Take":

* *var q =
* * *from tag in database.Tags
* * *where tag.Name.StartsWith(StartsWith)
* * *orderby tag.Name
* * *select tag;

* *List<Tagtags = (N 0 ? q.Take(N) : q).ToList();

And that's all there is to it. See alsohttp://msdn.microsoft.com/vcsharp/aa336746for examples of LINQ in action.

--
J.
Thank You!

Nice tip that web site ... I am just checking the examples.

Cheers,
Miguel
Jul 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.