473,396 Members | 2,158 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,396 software developers and data experts.

Create List in Linq

Hello,

I want to create a List(Of Tag) where Tag is an object with two
properties: ID and Name
I am creating this list from a CSV string that contains names of many
Tags.
So I create the List, giving to each Tag only its name (the ID remains
empty, as follows:

List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
p.Trim() }).ToList();

What happens is that if the CSV is empty I still get one Tag in the
list with Name = "".

What am I doing wrong?

Thanks,
Miguel
Aug 22 '08 #1
4 2229
shapper wrote:
Hello,

I want to create a List(Of Tag) where Tag is an object with two
properties: ID and Name
I am creating this list from a CSV string that contains names of many
Tags.
So I create the List, giving to each Tag only its name (the ID remains
empty, as follows:

List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
p.Trim() }).ToList();

What happens is that if the CSV is empty I still get one Tag in the
list with Name = "".

What am I doing wrong?
If you don't want empty strings then you can filter them out:

List<Tagform = paper.Tags.Split(',').Where(p =p != "").Select(p =>
new Tag { Name =
p.Trim() }).ToList();

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 22 '08 #2
Hi!

You can use the StringSplitOptions.RemoveEmptyEntries as 2nd paramter to the
Split method.

Regards,

Pierre T.

"shapper" <md*****@gmail.comwrote in message
news:64**********************************@e39g2000 hsf.googlegroups.com...
Hello,

I want to create a List(Of Tag) where Tag is an object with two
properties: ID and Name
I am creating this list from a CSV string that contains names of many
Tags.
So I create the List, giving to each Tag only its name (the ID remains
empty, as follows:

List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
p.Trim() }).ToList();

What happens is that if the CSV is empty I still get one Tag in the
list with Name = "".

What am I doing wrong?

Thanks,
Miguel
Aug 22 '08 #3
On Aug 22, 3:10*pm, "Pierre T." <pier...@pierretci.comwrote:
Hi!

You can use the StringSplitOptions.RemoveEmptyEntries as 2nd paramter to the
Split method.

Regards,

Pierre T.

"shapper" <mdmo...@gmail.comwrote in message

news:64**********************************@e39g2000 hsf.googlegroups.com...
Hello,
I want to create a List(Of Tag) where Tag is an object with two
properties: ID and Name
I am creating this list from a CSV string that contains names of many
Tags.
So I create the List, giving to each Tag only its name (the ID remains
empty, as follows:
List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
p.Trim() }).ToList();
What happens is that if the CSV is empty I still get one Tag in the
list with Name = "".
What am I doing wrong?
Thanks,
Miguel
You mean:

List<Tagform = paper.Tags.Split(new char[] {','},
StringSplitOptions.RemoveEmptyEntries).Select(t =new Tag { Name =
t.Trim() }).ToList();

It didn't work ... I still get one item ... Count = 1 of form list.

Am I doing something wrong?

Thanks,
Miguel
Aug 22 '08 #4
On Aug 22, 3:10*pm, "Pierre T." <pier...@pierretci.comwrote:
Hi!

You can use the StringSplitOptions.RemoveEmptyEntries as 2nd paramter to the
Split method.

Regards,

Pierre T.

"shapper" <mdmo...@gmail.comwrote in message

news:64**********************************@e39g2000 hsf.googlegroups.com...
Hello,
I want to create a List(Of Tag) where Tag is an object with two
properties: ID and Name
I am creating this list from a CSV string that contains names of many
Tags.
So I create the List, giving to each Tag only its name (the ID remains
empty, as follows:
List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
p.Trim() }).ToList();
What happens is that if the CSV is empty I still get one Tag in the
list with Name = "".
What am I doing wrong?
Thanks,
Miguel
My mistake ... both options worked! With StringSplitOptions and using
Where in Linq ... I suppose using StringSplitOptions is better in the
case ... I think ...

Thanks,
Miguel
Aug 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: =?Utf-8?B?VG9kZCBCZWF1bGlldQ==?= | last post by:
Hello, I have a list of elements and I want to perform an operation on a subset of them and then remove them from the list. If I build a sequence of elements via LINQ to drive the enumeration, as...
8
by: Andy | last post by:
Hi, I'm trying to add a where clause to my query: List<stringtypes = new List<string>(); types.Add( "A" ); types.Add( "B" ); query = query.Where( c =types.Contains( c.Type ) );
1
by: vangraan | last post by:
I am trying to use Linq to query a Dictionary of Objects based on values that is in a List inside the objects. I have a bunch of objects of class Photo stored in a Dictionary in these objects I...
0
by: Marc Gravell | last post by:
I would like to know how to write this in LINQ You don't need LINQ for this; you can sort the existing list using a Comparer<T>: List<InterfaceBaselist = new List<InterfaceBase>(); ...
3
by: =?Utf-8?B?YW1pcg==?= | last post by:
Hi, I have a Generic Object that has a private List field item. I populate the List in a different function I use a FOREACH LOOP with a FindAll function to get all items that have a certain...
9
by: CSharper | last post by:
I have a program which takes in various command line parameters and each prefixed with a letter for eg: /a:one /b:two etc., Currently I have a switch statement which phrases the parameter and...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
3
by: =?Utf-8?B?RGFiYmxlck5M?= | last post by:
I have the following Methods: public SecondResult DoSomething() { FirstResult firstResult=GetFirstResult(); SecondResult secondResult=GetSecondResult(firstResult); return secondResult; } ...
1
by: jags | last post by:
HI All I was trying to create Document librray programatically in a Sharepoint Site and is not working for me.I was trying to implement this as an Event Handler. The aim of the code is to create...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.