LINQ to Objects auto increment number - asp.net

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!

Related

how to use IN operator in LINQ

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...

Convert SQL to LINQ query to get max

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]");

Linq To Sql Get data into Label

I have a label to show BookName. I get it from table which name tblBooks. I don't know how to show Book Name into the label.
var query = from b in dc.tblBooks.Where(b=>b.BookID == 'B01') select b;
Can you help me know.
Your query as written will return a collection of books—IQueryable<Book>. If you're sure there will only be one result in this query, you can call SingleOrDefault, which will execute the query immediately and give you the actual book.
var Book = dc.tblBooks.Where(b => b.BookID == 'B01').SingleOrDefault();
if (Book != null)
myLabel.Text = Book.BookName;
Or you can simply say:
var Book = dc.tblBooks.SingleOrDefault(b => b.BookID == 'B01');
Which does the same thing.
If you're 110% sure there will always be a result, and you don't want to check for null, then you can use Single, which will do the same thing, except throw an exception if no results are found, where SingleOrDefault simple returns null.
var Book = dc.tblBooks.Single(b=>b.BookID == 'B01');
myLabel.Text = Book.BookName;
Try:
label.Text = query.FirstOrDefault().BookName;

Create a VB.NET Array with two columns of values?

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.

Can't use foreach on a anonymous type

I have the code below where I am trying to go through the child questions of my qestAires anonymous type. When I get to the foreach loop i get the error:
foreach statement cannot operate on
variables of type 'Question' because
'Question' does not contain a public
definition for 'GetEnumerator'
What do I need to do differently to get around this?
var questAires = (from qs in dc.Questionnaires
from q in dc.Questions.Where(t => t.QuestionnaireId == qs.QuestionnaireID)
from r in dc.Responses.Where(qr => qr.QuestionID == q.QuestionId).DefaultIfEmpty()
where qs.QuestionnaireID == QuestionnaireId
select new
{
qs.Description,
Questions = q,
Responses = r
}).Single();
foreach(var question in questAires.Questions)
{
}
questAires.Questions will only resolve to a single question, and you will get one questAires object for each question (which will cause .Single() to throw).
I guess you want something like this:
var questAires = (from qs in dc.Questionnaires
select new {
qs.Description,
Questions = from q in dc.Questions where q.QuestionnaireId == qs.QuestionnaireID
select new {
Question = q,
Response = (from r in dc.Responses where r.QuestionID == q.QuestionId select r).DefaultIfEmpty()
}
}).Single()
q actually resolves to a single item from the enumerable dc.Questions.Where(...), so yeah, you're only going to get a single item - not an enumerable - for Questions.

Resources