473,471 Members | 4,629 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linq. Select

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
2 3681
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: shapper | last post by:
Hello, I have 3 SQL tables: TagId, TagText PostId, TagId, ... FileId, TagId, ... I need to, using LINQ, select all records in Tags, including the columns TagId and TagText, but adding...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
7
by: Andy B | last post by:
Just wondering why linq is more useful than datasets? The stuff I do doesn't seem to be too complicated to use linq with it. If I did use linq with it now, I would be doing almost the exact same...
15
by: shapper | last post by:
Hello, I have two Lists: A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null, Peter) } B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) } I want to...
6
by: Plissskin | last post by:
I need to create an "ad-hoc" filtering page in a web app we are building. The page would display a number of drop down lists, text boxes, and radio lists and allow the user to select (enter) some...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
21
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
2
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.