Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call windows 10 virtual keyboard from windows forms app 2 32
SQL Help 27 40
.NET 2008 VB and C# 6 27
C# Gridview 1 27
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

816 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

12 Experts available now in Live!

Get 1:1 Help Now