Solved

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

Posted on 2010-08-13
4
439 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now