473,729 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ 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 3704
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.Starts With(StartsWith )
select tag

We want them ordered by Name:

from tag in database.Tags
where tag.Name.Starts With(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.Starts With(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...@xs4al l.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.Starts With(StartsWith )
* *select tag

We want them ordered by Name:

* *from tag in database.Tags
* *where tag.Name.Starts With(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.Starts With(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
8153
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 a new column of type boolean
4
549
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.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
7
2668
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 programming, just with a different data getting process...
15
6035
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 find which items in A do not exist in B then:
6
4167
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 data that would be used to filter the dataset we return. STANDARD STUFF. Below is simplified version (not compete) of how we solved this with ADO using javascript on the client to build a where clause that we would use on our SQL SELECT...
14
3688
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
11587
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
4359
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
2
2397
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
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.