I have list object and I need to check id in a comma separated string using LINQ in VB.NET, something like this:
dim strId as String = "1,2,3,5,"
dim myList = from objmylist where objmylist.id in (strId)
dim strId as String = "1,2,3,5,"
dim IDs as String() = strId.Split(",")
dim myList = from objmylist where IDs.Contains(objmylist.id)
select objmylist
dim strId as String = "1,2,3,5,"
dim myList = from objmylist where strId.split(",").Contains(objmylist.id)
untested, but should do the trick I guess.
Your code is perfectly fine if you split the string before using it in the linq
In C# you would do this:
string strIDs = "1,2,3,5,";
string[] arrIDs = strIDs.Split(",");
var myList = objmylist.Where(o => arrIDs.Contains(o.id));
Perhaps you can understand this enough to translate it into VB
Using C#,
int[] productList = new int[] { 1, 2, 3, 4 };
var myProducts = from p in db.Products
where productList.Contains(p.ProductID)
select p;
using VB.NET,
Dim productList As Integer() = New Integer() {1, 2, 3, 4}
Dim myProducts = From p In db.Products Where productList.Contains(p.ProductID)
In Reference from Creating SQL IN Queries using LINQ
Related
I have a folder with multiple text files in it, each text files has about 14 lines of text.
I would like to add all text files in that folder to a gridcontrol/gridview in vb.net.
My current code only adds 1 text file instead of adding all. any help would be greatly appreciated.
Dim path As String = "C:\Plan\"
For Each i As String In System.IO.Directory.GetFiles(path)
Dim a, b, c As String
a = System.IO.Path.GetFileNameWithoutExtension(i)
b = System.IO.Path.GetFileName(i)
c = System.IO.Path.GetFullPath(i)
Dim LINE_pair As String = IO.File.ReadLines(i).ElementAtOrDefault(0)
Dim LINE1_details As String = IO.File.ReadLines(i).ElementAtOrDefault(1)
Dim LINE2_outlookcombo As String = IO.File.ReadLines(i).ElementAtOrDefault(2)
Dim LINE3_rsicombo As String = IO.File.ReadLines(i).ElementAtOrDefault(3)
Dim LINE4_macdcombo As String = IO.File.ReadLines(i).ElementAtOrDefault(4)
Dim LINE4_ratio As String = IO.File.ReadLines(i).ElementAtOrDefault(5)
Dim LINE5_pattern As String = IO.File.ReadLines(i).ElementAtOrDefault(6)
Dim LINE6_none As String = IO.File.ReadLines(i).ElementAtOrDefault(7)
Dim LINE7_timeframecomvo As String = IO.File.ReadLines(i).ElementAtOrDefault(8)
Dim LINE7_date As String = IO.File.ReadLines(i).ElementAtOrDefault(9)
Dim LINE8_trade As String = IO.File.ReadLines(i).ElementAtOrDefault(10)
Dim LINE9_currentprice As String = IO.File.ReadLines(i).ElementAtOrDefault(11)
Dim LINE10_tp As String = IO.File.ReadLines(i).ElementAtOrDefault(12)
Dim LINE11_sl As String = IO.File.ReadLines(i).ElementAtOrDefault(13)
Dim NewItem As New ListViewItem(a)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(13) {New DataColumn("Pair"), New DataColumn("Outlook"), New DataColumn("RSI"), New DataColumn("MACD"), New DataColumn("Pattern"), New DataColumn("Misc"), New DataColumn("Rato"), New DataColumn("Time Frame"), New DataColumn("Date"), New DataColumn("File name"), New DataColumn("Trade Status"), New DataColumn("CP"), New DataColumn("TP"), New DataColumn("SL")})
dt.Rows.Add(New String() {LINE_pair, LINE2_outlookcombo, LINE3_rsicombo, LINE4_macdcombo, LINE5_pattern, LINE6_none, LINE4_ratio, LINE7_timeframecomvo, LINE7_date, a, LINE8_trade, LINE9_currentprice, LINE10_tp, LINE11_sl})
GridControl1.DataSource = dt
It is not necessary to declare the DataColumn array explicitly. It is created internally from the items in the braces.
You are throwing away your NewItem on each iteration. Why not add them to a list and add them all at once to a ListView.
You are reading the file over and over as you assign the LINE_ data. Read it once and use the resulting lines array.
You never use b and c in this code so I deleted them
I tested in with a .net DataGridView. The file io methods and the DataTable are the same.
Private Sub OPCode()
Dim path As String = "C:\Plan\"
Dim dt As New DataTable()
Dim lstListViewItems As New List(Of ListViewItem)
dt.Columns.AddRange({New DataColumn("Pair"), New DataColumn("Outlook"), New DataColumn("RSI"), New DataColumn("MACD"), New DataColumn("Pattern"), New DataColumn("Misc"), New DataColumn("Rato"), New DataColumn("Time Frame"), New DataColumn("Date"), New DataColumn("File name"), New DataColumn("Trade Status"), New DataColumn("CP"), New DataColumn("TP"), New DataColumn("SL")})
For Each i As String In System.IO.Directory.GetFiles(path)
Dim a = System.IO.Path.GetFileNameWithoutExtension(i)
Dim lines = File.ReadLines(i)
Dim LINE_pair As String = lines.ElementAtOrDefault(0)
Dim LINE1_details As String = lines.ElementAtOrDefault(1)
Dim LINE2_outlookcombo As String = lines.ElementAtOrDefault(2)
Dim LINE3_rsicombo As String = lines.ElementAtOrDefault(3)
Dim LINE4_macdcombo As String = lines.ElementAtOrDefault(4)
Dim LINE4_ratio As String = lines.ElementAtOrDefault(5)
Dim LINE5_pattern As String = lines.ElementAtOrDefault(6)
Dim LINE6_none As String = lines.ElementAtOrDefault(7)
Dim LINE7_timeframecomvo As String = lines.ElementAtOrDefault(8)
Dim LINE7_date As String = lines.ElementAtOrDefault(9)
Dim LINE8_trade As String = lines.ElementAtOrDefault(10)
Dim LINE9_currentprice As String = lines.ElementAtOrDefault(11)
Dim LINE10_tp As String = lines.ElementAtOrDefault(12)
Dim LINE11_sl As String = lines.ElementAtOrDefault(13)
dt.Rows.Add({LINE_pair, LINE2_outlookcombo, LINE3_rsicombo, LINE4_macdcombo, LINE5_pattern, LINE6_none, LINE4_ratio, LINE7_timeframecomvo, LINE7_date, a, LINE8_trade, LINE9_currentprice, LINE10_tp, LINE11_sl})
Dim NewItem As New ListViewItem(a)
lstListViewItems.Add(NewItem)
Next
ListView1.Items.AddRange(lstListViewItems.ToArray)
DataGridView1.DataSource = dt
End Sub
I'm trying to automate test some codes and I can't seem to get this working on ListObject.
If I run the test it fails with an error:
Value of type String() cannot be converted into ArrayList.
Here's what I'm trying:
C# CODE:
public string[] GetUserIdsFromPassId(string organisationId, string #PassId)
{
DbParameterCollection parameters = new DbParameterCollection();
parameters.Add(new DbParameter("#OrganisationId", SqlDbType.NVarChar, organisationId));
parameters.Add(new DbParameter("#PassId", SqlDbType.NVarChar, #PassId));
string sql = "SELECT UserId FROM Orchestra WHERE OrganisationId=#OrganisationId AND PassId=#PassId";
ListObject list = new ListObject(_Accessor);
list.Read(sql, parameters);
List<string> userIds = new List<string>();
foreach (DataRow dataRow in list.Table.Rows)
userIds.Add(dataRow["UserId"].ToString());
return userIds.ToArray();
}
AUTOMATE TESTING CODE:
<TestClass()> Public Class Check_UserIdsFromPassId
<TestMethod()> Public Sub GetUserIdsFromPassId()
Dim organisationId As String = "1123"
Dim PassId As String = "8110004"
Dim UserId As string = String.Empty
Dim ExpUserId As String = "00044"
Dim DataServer As New DataServer()
Dim Accessor = DataServer.GetAccessor()
Dim _StandardHeader = New StandardHeader
Dim _AuditProvider = New Audit.AuditProvider(_StandardHeader)
Dim AD As New Ceridian.Administration.Authentication.AuthenticationData(Accessor, _AuditProvider)
UserId = AD.GetUserIdsFromPassId(organisationId, PassId)
Assert.AreEqual(ExpUserId, UserId)
Console.WriteLine(ExpUserId)
Console.WriteLine(UserId)
End Sub
End Class
If you are expecting the function under test to return a string array with a single UserId (ExpUserId) you could just test the length of the array and the first value.
For instance, remove this line:
Dim UserId As string = String.Empty
Change the line that exercises the function under test to:
Dim UserId = AD.GetUserIdsFromPassId(organisationId, PassId)
and then confirm the returned array has a single correct value:
Assert.AreEqual(1, UserId.Count)
Assert.AreEqual(ExpUserId, UserId(0))
I have some C# LINQ code and would like to convert it to vb.net. Can you help?
var userSessionId= 25;
ProjectsPerUser = db.tbProjekt.Where(s => s.tbUserProjects.Any(x => x.UserId == userSessionId)).ToList();
it was out of this raw sql query:
ProjectsPerUser = db.Database.SqlQuery(Of tbProjekt)("SELECT * FROM [16281468_general].[dbo].[tbProjekt] WHERE Id " _
& "IN (SELECT DISTINCT ProjectId FROM [16281468_general].[dbo].[tbUserProject] WHERE UserId = " & userSessionId & ")").ToList
now I want to convert it to vb.net but I have some problems with that.
That is my current try:
tables within context: tbProjekt and tbUserProjects
Dim dabcon As New production_TextEntitesContext
Dim ProjectsPerUsers As New List(Of tbProjekt)
Dim userSessionId As Integer = 111
ProjectsPerUsers = dabcon.tbProjekt.Where(Function(s) s.tbUserProjects.Any(Function(x) x.UserId = userSessionId)).ToList()
But, I got this error:
Late binding operations cannot be converted to expression tree
on line s.tbUserProjects.Any.
Maybe something like this would help. Just an example
Sub Main()
Dim projects As New List(Of Integer)
projects.Add(1)
projects.Add(2)
projects.Add(3)
Dim users As New Dictionary(Of String, Integer)
users.Add("john", 1)
users.Add("mary", 2)
Dim userName As String = "john"
Dim userProjects = From p In projects
Join u In users On p Equals u.Value
Where u.Key = userName
Select p, u.Key
For Each v In userProjects
Console.WriteLine("user: {0} project: {1}", v.Key, v.p)
Next
Console.ReadLine()
End Sub
Assuming you have navigation properties set up, this should work as well:
Dim userProjects=dabcon.tbUsers.Where(Function(u) u.Key=userName).Select(Function(u) u.Projects))
or query syntax:
Dim userProjects=from u in dabcon.tbUsers
where u.Key=userName
select u.Projects
Getting an error on this code which is part of a function to receive a message, it takes the querystring values and splits them or at least is supposed to right now it gives me an index out of range trying to assign the value to paramValue as string=params(1).ToString and I am not sure why?
Dim QueryString As String() = context.Split("&")
Dim QuerySet As String
Dim Params As String() = Nothing
For Each QuerySet In QueryString
Params = QuerySet.Split("=")
Dim ParamKey As String = Params(0).ToString.ToLower
Dim ParamValue As String = Params(1).ToString
Not an answer, but won't fit as a comment:
For Each QuerySet As String() In context.Split("&"c)
Dim Params As String() = QuerySet.Split("="c)
Next
Add some error checking to your code:
Dim QueryString As String() = context.Split("&"c)
Dim QuerySet As String
Dim Params As String() = Nothing
For Each QuerySet In QueryString
If QuerySet.Contains("=") Then 'procede
Params = QuerySet.Split("="c)
Dim ParamKey As String = Params(0).ToLower
Dim ParamValue As String = Params(1)
'use variables
End If
Next
i have this string
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
and want to retrieve a string
newstr = 12,32,15,16,14
i tried this much
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim word As String
Dim uc As String() = test.Split(New Char() {","c})
For Each word In uc
' What can i do here?????????
Next
only unique numbers how can i do that in vb asp.net
right answer
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim word As String
Dim uc As String() = test.Split(New Char() {","c}).Distinct.ToArray
Dim sb2 As String = "-1"
For Each word In uc
sb2 = sb2 + "," + word
Next
MsgBox(sb2.ToString)
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim uniqueList As String() = test.Split(New Char() {","c}).Distinct().ToArray()
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
'Split into an array
Dim testArray As String() = test.Split(",")
'remove duplicates
Dim uniqueTestArray As String() = testArray.Distinct().ToArray())
'Concatenate back to string
Dim uniqueString As String = String.Join(",", uniqueTestArray)
Or all in one line:
Dim uniqueString As String = String.Join(",", test.Split(",").Distinct().ToArray())
Updated Sorry I forgot to add the new string together
Solution:
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim distinctArray = test.Split(",").Distinct()
Dim newStr As String = String.Join(",", distinctArray.ToArray())
Training References: Check out this website for a guide on LINQ which will make these types of programming challenges easier for you. LINQ Tutorial
You forgot to put parentheses for Distinctand ToArray. Because these are methods
Dim uc As String() = test.Split(New Char() {","c}).Distinct().ToArray()