Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

20 Experts available now in Live!

Get 1:1 Help Now