Linq. Select

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following LINQ:

List<Tag> tags = (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
 
shapper said:
I have the following LINQ:

List<Tag> tags = (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<Tag> tags = (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.
 
StartWidth or StartsWith? The latter would make more sense.


Alphabetically by what? I assume "Name".



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<Tag> tags = (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.

Thank You!

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

Cheers,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq > Group 2
Linq Order By 2
List conversion 2
List to CSV 3
(Collection) 1
Linq Query. Please, help. Going crazy ... 1
Linq. Where 10
Linq. Take and OrderBy 7

Back
Top