I have the list of ProjectId like 14,15,18 and i want to search those items from my datatable using linq.
now i have created the query like
IEnumerable<DataRow> drProjList = from a in dtProj.AsEnumerable()
where a.Field<int>("ProjectId")
.ToString()
.Contains(projFilter)
select a;
but i am not getting the 0 elements.
can any body suggest me any other way like IN operator in linq.
thanks in advance
EDIT
projFilter is the pure string which have the 14,15,18,... project ids.
String's "contains" isn't really an option here. You should convert projFilter into a proper list of integers first.
var projFilter2 = new HashSet<int>(projFilter.Split(',').Select(i => int.Parse(i)));
and only then check if it contains the desired number.
IEnumerable<DataRow> drProjList =
from a in dtProj.AsEnumerable()
where projFilter2.Contains(a.Field<int>("ProjectId"))
select a;
I don't know much about the AsEnumerable(), but I would have done it the other way round, something like this:
List<int> intProjFilter = new List<int>();
foreach(string filter in projFilter.Split(','))
intProjFilter.Add(int.Parse(filter));
from a in dtProj
where projFilter.Contains(a.ProjectID)
select a;
Let me know if this helps
You need to parse the project filter into collection of integers first.
var projectIds = projFilter.Split(',').Select(x => Convert.ToInt32(x)).ToList();
var drProjList =
from a in dtProj.AsEnumerable()
where projectIds.Contains(a.ProjectId)
select a;
Maybe something like this:
IEnumerable<DataRow> drProjList =
(
from a in dtProj.AsEnumerable()
where projFilter.Contains(a.Field<int>("ProjectId").ToString())
select a;
)
Or you can simply do this as well:
IEnumerable<DataRow> drProjList=dtProj.AsEnumerable()
.Where(a=>projFilter
.Contains(a.Field<int>("ProjectId")
.ToString()));
I don't know c# well so i am trying to give answer in vb.net hope that will help you.
Dim dtProj As DataTable
dtProj = FillProjectList()
Dim projIdFilter() As Integer = {14, 15, 16}
Dim drRow = From p In dtProj.AsEnumerable _
Where projIdFilter.Contains(p.Field(Of Integer)("ProjectId")) _
Select p
Thank you...
Related
I need help about select a row in a kendoGrid.
I have a simple kendoGrid with selection enabled, and when i click on a button in a webpage, i have to use a string (for example "cod001") for selecting a row in my kendogrid by a column....
for example:
var grid = $("#grid").data("kendoGrid");
grid.select("??????????");//here i sould select a row where the unique value is "cod001" in a defined column
hope someone can help me.
thanks in advance.
I find an alternative solution, without each functions...
i'll post my solution, and hope can help somoeone with my same issues!!!
var g = $("#grid").data("kendoGrid");
var selectedRow = g.select();
var index = selectedRow.index();
... and then...
var ddl = $("#grid").data("kendoGrid");
ddl.select("tr:eq(" + index + ")");
you can make a loop on each line of your grid to check what the column your looking for is and then select it.
var linesToSelect = [];
$.each($('.k-grid-content tbody').children(), function(index, line){
// column is the column's value you want to test
if ($("#grid").data("kendoGrid").dataItem(line).column == "cod001")
linesToSelect.push(line);
});
$("#grid").data("kendoGrid").select(linesToSelect);
This is not a perfect solution since you do a loop on every rows of your grid, but it should help until you find a better solution!
I want to select one item in drop down list in ASP.NET written with VB.NET - I have values and texts in listbox like this:
Volvo
Audi
etc...
But values coming from other place in upper case... VOLVO, AUDI..
This code:
dropdownlist.FindByValue("CAPITAL")
Is not working and giving null for Volvo.. please help.
One way would be LINQ:
Dim volvoItem = dropdownlist.Items.Cast(Of ListItem)().
FirstOrDefault(Function(i) i.Text.Equals("Volvo", StringComparison.InvariantCultureIgnoreCase))
C#:
var volvoItem = dropdownlist.Items.Cast<ListItem>()
.FirstOrDefault(i => i.Text.Equals("Volvo", StringComparison.InvariantCultureIgnoreCase));
This worked for me
foreach(ListItem li in dropdownlist.Items)
{
if (String.Compare(li.Text, myLabel.Text, true) == 0)
myCustomValidator.IsValid = false; // Match Found !
}
Like Tim said LINQ would be you answer.
in C# try the following:
var item = dropdownlist.Items.Cast<ListItem>().SingleOrDefault(li =>
li.Text.ToUpper() == "VOLVO");
Additional variants, using framework CI comparison. VB:
uiList.Items.Cast(Of ListItem)
.FirstOrDefault(Function(i) i.Text.Equals(comparand, StringComparison.InvariantCultureIgnoreCase))
C#:
uiList.Items.Cast<ListItem>()
.FirstOrDefault(i => i.Text.Equals(comparand, StringComparison.InvariantCultureIgnoreCase));
You could also use CurrentCultureIgnoreCase depending on your requirements. These are generally safer than comparing with ToUpper/ToLower, because some cultures have unexpected casing rules.
hi guys i am stuck converting below sql to LINQ query.
all i want is to have maximum number from list of (FA-00001 ,FA-00059)
SELECT MAX(CAST(SUBSTRING(ReferenceId, PATINDEX('%-%', ReferenceId) + 1, LEN(ReferenceId) - PATINDEX('%-%', ReferenceId)) AS int)) AS MaxReferenceId FROM [ClientRC].[dbo].[EHO_Action]
is this possible to convert to LINQ? thanks
An alternative approach using anonymous projection:
var y = (from record in
(from record in db.ClientRC
select new
{
Group = "x",
ReferenceNumber = Convert.ToInt32(record.ReferenceId.Split('-')[1])
})
group record by new { record.Group } into g
select new
{
MaxReferenceId = g.Max(p => p.ReferenceNumber)
});
http://msdn.microsoft.com/en-us/library/bb386972.aspx
var myvar = (from v in db.object where v!=null select v.id).Max();
MSDN has lots of examples for stuff like this.
Or, you can execute queries directly against a datacontext if you're using entity framework. Just make sure if you're doing anything with parameters you're parameterizing the query and not taking user input directly into it.
http://msdn.microsoft.com/en-us/library/ee358769.aspx
Try this..
var list = DBContext.EHO_Action
.Where(x => x.YourListColumn != null)
.Select(x => x.YourListColumn).ToList(); // Take the list (FA-00001 ,FA-00059) from db to a list
var maxNo = list.Max(x => Convert.ToInt32(x.Split('-')[1]));
Please change the context and column names according to your Linq context.
If you want to use sql you can do it this way..
var list = DBContext.ExecuteQuery<string>("select yourreqrdcolumn from [ClientRC].[dbo].[EHO_Action]");
I know how to create an array and loop through it normally - but what if I need a multi-column array. e.g. usually I might do something like:
For Each row in NameofArray
Dim name as String = row
Response.Write("Hello " & name & "!")
Next
But what if I want to do something like:
For Each row in NameofArray
Dim name as String = row.name
Dim age as Integer = row.age
Response.Write("Hello " & name & "! You are " & age & " years old!"
Next
If this isn't possible with an array, is there another way I can accomplish this?
Create your custom data type:
public struct DataType
public string Name;
public int Age;
}
Such type you can than use in an array like that:
DataType[] myData = new DataType[100];
myData[0].Name = "myName";
myData[0].Age = 100;
Note, if looping through that array via foreach, the elements returned for each iteration cannot get altered. If this is an requirement for you, consider using 'class' rather than 'struct' in the above DataType declaration. This will come with some other implications though. For example, the instances of a class DataType will explicitely have to be created via the 'new' keyword.
After reading your comment I think my other answer is probably what you are looking for.
What type is row and what type is NameOfArray?
If you would like to make row into a coumpound type with several members then there a several options.
Structure Row
Public Name as String
Public Age as Integer
End Structure
for instance. If you would prefer a reference type substitute Class for Structure.
Or using anonymous types,
Dim row = New With {Name = "Bob", Age = 21}
Then you can use generics to make a list of rows that you can iterate through using ForEach.
Dim NameOfList As System.Collections.Generic.List(of Row)
or if it were a result of a LINQ query somthing that supported
IEnumerable(of New With{Name As String, Age As Int}). //Not sure if this is VB
I'm not certain I uderstand your question and hope this is the kind of thing you were looking for.
As you can see from my fellow answerers, the support for anonymous types is superior in C# but, since you asked the question in VB.Net I will limit myself to that context.
After reading your comment I think I understand the question.
You can do
///Spacer Top
Dim NameOfArray = {New With {.Age = 21, .Name = "Bob"}, New With {.Age = 74, .Name = "Gramps"}}
///Spacer Bottom
If you want to create an IEnumberable anonymous type of Name Age tuples ;-p
Did you tried Dictionary Class. You can loop through the Dictionary using KeyValue pair class.
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
new Dictionary<string, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
foreach(var item in openWith)
{
Console.WriteLine(item.Key +" can be open with " + item.value);
}
You need to (can) index into your array using the two dimensions ie...
Dim array(,) As Object = { _
{"John",26}, _
{"Mark",4} _
}
For row As Integer = 0 to array.GetUpperBound(0)
Dim name as String = CStr(array(row,0))
Dim age as Integer = CInt(array(row,1))
Response.Write("Hello " & name & "! You are " & age & " years old!")
Next
Though would be better storing this sort of information in a class or user defined type of some kind.
This feels like a completely basic question, but, for the life of me, I can't seem to work out an elegant solution.
Basically, I am doing a LINQ query creating a new object from the query. In the new object, I want to generate a auto-incremented number to allow me to keep a selection order for later use (named Iter in my example).
Here is my current solution that does what I need:
Dim query2 = From x As DictionaryEntry In MasterCalendarInstance _
Order By x.Key _
Select New With {.CalendarId = x.Key, .Iter = 0}
For i = 0 To query2.Count - 1
query2(i).Iter = i
Next
Is there a way to do this within the context of the LINQ query (so that I don't have to loop the collection after the query)?
Pardon me for doing this in C# not sure exactly the syntax in VB.NET:
MasterCalendarInstance
.OrderBy(x => x.Key)
.Select((x, ixc) => new { CalendarId = x.Key, Iter = ixc });
I don't know if this is possible in VB, but in C# one uses a closure:
int count = 0
var res = from x in MasterCalendarInstance
order by x.Key
select new {
CalendarId = x.Key,
Iter = count++
};
The above solutions could be summed up in VB.NET like this :
MasterCalendarInstance _
.OrderBy(Function (x) x.Key) _
.Select(Function (x, ixc) New With { .CalendarId = x.Key,
.Iter = ixc })
I ran into this post while trying to solve a similar problem with a List(of String).
I'm posting my workaround in hopes that it may be adopted to resolve your issue, but more for anyone else who runs into this issue with a List(Of T).
Dim list As New List(Of String)
list.Add("Test1")
list.Add("Test2")
list.Add("Test3")
Dim var = list.Select(Function(s) New With {.Name = s, .RecordID = list.IndexOf(s)})
Hope this helps!