Solved

How to create and fill a list inside a LINQ query?

Posted on 2010-08-13
4
449 Views
Last Modified: 2013-11-11
Hi experts

I would like to use LINQ from a datatable to  create a an object that contains a generic list

first i have this class :


    Public Class Transition
        Public Property Id As Int32
        Public Property Name As String
        Public Property Keywords As List(Of String)
        Public Property Description As String
    End Class

then i have this datatable

id | name | keywords | description

keywords column contains string like :

"string1/string2/string3/String4"

then i have to split to have 4 string in the list of keywords

that's why i would like to create a linq to do this in one shot :


   lQuery = From dr As DataRow In lDataTable.AsEnumerable _
                  Select New Transition() With {.Id = Convert.ToInt32(dr("Id")), .Name = dr("Name").ToString.Trim, .Description = dr("Description").ToString.Trim, .Keywords = ???}


Any ideas?


Thank you,

Regards.
0
Comment
Question by:Dnx_7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
naspinski earned 500 total points
ID: 33428198

.Keywords = dr("Keywords").ToString().Split('/').ToList()

Open in new window

0
 
LVL 2

Author Comment

by:Dnx_7
ID: 33428562
Oh my god, thank you!

But should i ask another question lol...

How can i check the content of the dr("Keywords") before splitting it?
because in some case, the content has a value of NOTHING or is empty...

do the "IIF" makes the trick?
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 33428590
Nevermind,

i used this command, it works :

.Keywords =  DirectCast(IIf(String.IsNullOrEmpty(dr("KeywordList").ToString), New List(Of String), dr("KeywordList").ToString.Split("/"c).ToList), List(Of String)


Thank you!
0
 
LVL 21

Expert Comment

by:naspinski
ID: 33428683
not sure if VB supports ternary operators, but in c# I would do something like this:
.Keywords = (dr("Keywords") == null ? new List(of string) : dr("Keywords").ToString().Split('/').ToList())

//which is the same as:
//if keywords is null the use and empty list, else, parse it

Open in new window

0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question