473,378 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 3678
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 {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.