?
Solved

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

Posted on 2010-08-13
4
Medium Priority
?
452 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 2000 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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