Is There A Better Way To Do LINQ Statement Block
Aug 13, 2009
I'm relatively new with LINQ, but I'm going to be getting into it a lot more. Is the following a practical application of LINQ, or is there a better way to do something like this? [code] I was thinking if I could get the property name somehow as an object I could just do one LINQ statement,
View 5 Replies
ADVERTISEMENT
Oct 5, 2009
how to use them then before, but am still a little confused with a few aspects. Here goes:
1.) Lets say you have a method that checks for a certain condition(s) and if it fails Throws an exception. Does it have to be in a try/catch block? Meaning can the "Throw" statement exist in a block with no try/catch statement?
2.) Now lets say we have a method that has a try catch block and in it there is a throw statement. When the throw statement is executed does it first try to find an appropriate catch block in the same method or does it immediately go back to the calling method without looking at the catch statements in the current method where the exception was thrown?
3.) I created a custom exception class that inherits from ApplicationException. Next I created a method which has a catch block that catches this type of exception and does some action. Is the System(i.e CLR) smart enough to throw an exception of this type, or does it only throw exceptions from SystemException?
4.) I know that some people are more liberal in their use of exceptions and others use it more sparingly when truly strange stuff happen like the DB going down. I am writing code where I am getting some info back from the database, converting it and then storing it. Sometimes there might be data that comes back from the database and other times the field is empty and the Null value comes back. Therefore in the instances where Null comes back from the database I should not convert the value to anything, since I will get an error. What should I do in this situation? Should I let the CLR throw the exception when it goes to convert the Null value or should I check for the Null value and if it exists not convert?
5.) In general when I throw exceptions, is it sensible to only throw exceptions of the type Application Exception or are there instances where the programmer throws exceptions of the type SystemException?
View 2 Replies
Jul 14, 2009
Studying Ruby and the Do block. Coming from much c# I didn't see much that reminded me of Do, but then VB came to mind with the With statement and one which I wish was in c# (maybe it is and I never saw it?). The two statements, Do and With, appear similar.Is the With statement in VB the same as a Do block in Ruby? [code]I understand what I will get just from looking at it. I know that I will get a new record in my database with the above information, but I do not know why. Why did book become TheBook.new?
View 5 Replies
Jun 24, 2011
I have the following SQL:
[Code]...
I want to put it (the select count statement) in this LINQ statement so I can get the sales count in my linq statement: Dim TheLeads = (From L In DB.Leads Where L.IsDeleted = False Select L).ToList() Is this possible to do in LINQ?
View 1 Replies
Dec 22, 2009
It's just before while I found that expression (see my signature) is possible, so this request is not about what 'I am missing', but what I can think of. It would be nice to have named sections of code in visualbasic, which would replaced almost everything you can miss about GoTo. Imagine this:
Section Compression
For Each item ...
If Not item IsV...
[code]....
View 4 Replies
Jun 24, 2011
Ok im trying to do a if statement in Linq and was wondering if it was possible to do something like:
Dim loadFriends = From p In db.UserRelationships Where p.aspnet_User.UserName = User.Identity.Name _
Or p.aspnet_User1.UserName = User.Identity.Name And p.Type = 1 _
Select New With {if p.aspnet_user1.user = "a certan username" then .username = _
p.aspnet_user.username else .username = p.aspnet_user1.Username}
[Code]...
View 1 Replies
Oct 7, 2009
I am going to be performing an XSLT transformation, transforming XML to an HTML table. It's tabular data, so that's why I'm not using div's.
[Code]...
View 3 Replies
Mar 28, 2012
I have a Customer object which has a collection of ContactNumbers. Is it possible with LINQ to get a list of Customers where one of the contact numbers = '123'?
Public Class Customer
Public Overridable Property ContactNumbers As List(Of ContactNumber)
End Class
[Code].....
View 1 Replies
Oct 8, 2009
I only want to return a certain number of rows into the DataTable via LINQ's Take or use it on the Rows property, but I am not sure how or where to do it: Here is the current code:
Dim dt As DataTable = GetDataTable("sp", params)
For Each dr As DataRow In dt.Rows
Dim o As New OR()
[code].....
View 1 Replies
Jun 16, 2011
I have something that I want to do and what looks very simple, but I cannot get it to work. I want to build a function that gets as input a xdocument and some other strings. It should have a return that contains a filtered part of the xdocument in a way that I can use it as input for a new Linq function. This is what I mean:
Private Function fncGetFilteredDecendantsOfNode(ByVal xdocDoc As XDocument, _
[Code]...
View 2 Replies
Sep 30, 2009
How can I turn the following query into a LINQ statement? [code]
View 1 Replies
Jul 15, 2010
I have the following code:[code]When I run it, looking at the debugger everything looks okay but when I check the database table it is supposed to be inserting into - the column values (for occupantnum) haven't changed - they are all still null! Not sure if this is related, but note that I've now updated j to explicitly be an integer. I noticed while debugging that wResident.occupantnum was appearing as Type "Integer?" - but this doesn't seem to have made any difference - when I rerun it it still says Type "Integer?"
View 4 Replies
May 18, 2012
Dim gg = From gs In AllData_TableList Order By gs.Biology Descending, gs.ChemistryDescending, gs.English Descending, gs.Frensh DescendingAll I need is to make this Statement order Dynamically like this
Dim Variable1 as string=Biology
Dim Variable2 as string=Chemistry
Dim Variable3 as string=English
[code].....
View 6 Replies
Oct 15, 2010
We use LINQ to SQL extensively, and one of the biggest performance pitfalls we've run into is situations where a query can't be converted to SQL, and so an entire database table gets loaded into memory and the query performed by .NET. For example, this query
Dim Foo = (From c in Db.Contacts Select c Where c.ContactID=MyContactID)
translates to a simple SQL query, whereas
Dim Foo = (From c in Db.Contacts Select c Where c.ContactID=SafeInt(MyContactID))
doesn't because SafeInt is our own function and doesn't have a SQL equivalent; so the whole contacts table gets loaded into memory and searched by LINQ to objects, which is vastly slower. With a little experience, we've gotten better at avoiding this kind of situation. But it's not always obvious, and this is the sort of problem that's often not revealed until you're working with a lot of data and things start to slow down. Is there a straightforward way to see - whether in Visual Studio or some other way - how LINQ queries will be handled, other than trial and error?
View 3 Replies
Dec 14, 2009
I have this LINQ statement [code]...
Is there any way that I can use if/iif within select ?
View 1 Replies
Oct 28, 2009
I'm just getting my feet wet with LINQ but am struggling to convert this SQL statement to LINQ.
Dim SQLString As String = "SELECT MESSAGE_ID FROM " & Schema & _
".PPM_TRANSFER_EMAIL WHERE TO_CHAR(IMPORTED_DATE,'YYYY/MM/DD') <= '" & _
DeletionDate.ToString("yyyy/MM/dd") & "' AND IMPORTED = 'Y' AND FILE_NAME LIKE '" & _
mb & "%'"
I have a Dataset that already contains all the data from the PPM_TRANSFER_EMAIL table so it seems silly to query the database again when I can use LINQ to query the dataset.
View 1 Replies
Nov 27, 2010
OK So I know WHY I am having the error I am getting. I don't know HOW to fix it. Basically, if the user doesn't have a certain permission, I need to join another table. But .NET is so picky I can't just make two different queries in an IF statement and then use it outside of the if statement. I can think of some ugly work arounds for this, but I would rather not. I am fairly new to .NET I know just enough to be dangerous.
[Code]...
because of this: Dim l As IEnumerable(Of Company) its not just IEnumerable of a Company, its got the source in it. I have to explicitly select source to use it in my datagrid. Can I make Dim l something that will make it stop complaining?
View 2 Replies
Nov 23, 2009
I have a listbox that is dynamically filled by certain values depending on what table has been selected from another list box. Once a value is selected, it is being graphed vs a date & time range. With my ignorance of linq: depending on what value is selected, the linq to sql statement I need to create to grab data from my database is different as I can't use an index on an anonymous type.
result = From t In db.APS _
Where t.DateTime >= startDate And _
t.DateTime <= finishDate And t.Weight = weight _
Select t.DateTime, t.TotalConcentration
t.TotalConcentration should be selected if my listbox value is "Total Concentration", but if it's something else, like "Temperature" or "Flow Rate" (connected to appropreate database columns) - this method obviously isn't going to work. I need to be able to dynamically select a specific column from the anonymous type list, or use some other method I'm unaware of to do this.
View 1 Replies
Apr 15, 2011
I have the following function
Public Function GetPositionBySchoolID(ByVal SchoolID As Integer) As CPAPositionsDataTable
Dim positions As New CPAPositionsDataTable
[Code]....
And I was wondering if there is any way to cut out assigning the LINQ result to tmp and just work with the result directly, i.e.
Public Function GetPositionBySchoolID(ByVal SchoolID As Integer) As CPAPositionsDataTable
Dim positions As New CPAPositionsDataTable
[Code].....
View 2 Replies
Sep 21, 2010
If txtSearchString.Text.Trim <> "" Then
Dim searchString As String = txtSearchString.Text.Trim
Dim results As EnumerableRowCollection(Of DataRow) = From PO In FilterPurchaseOrders().AsEnumerable
[CODE]..............
I am getting this error: error BC30205: End of statement expected.
FilterPurchaseOrders() function returns a datatable.
Whats wrong with the above code?
View 2 Replies
Jun 23, 2010
I thought this would have been quite common but can't find anything on this. I'm trying to query columns that have spaces immbedded within them. For the life of me I don't see a way of selecting them when I'm trying to assign them to an alias when creating an anonymous type result. [code]...
View 1 Replies
Aug 19, 2010
i want to use a if, then statement in a where clause in linq to sql. Is this possible? I'm trying to achieve the following:
[Code]...
I'm trying to only add the And If if intLink_pk > -1. If it = -1, then i don't want the and clause at all. Is this posssible within the linq to sql itself?I understand i can write 2 entirely different selects, but if I can't get this done, I may have to write about 50 of them as I want to use multiple Ands if certain conditions are met on the form, such as chk boxes etc.
View 3 Replies
May 11, 2012
In my programming task I've gone down a dark alley and wished I hadn't, but there is no turning back now.
I'm building up a SQL statement where the table name, column name and id value are retrieved from query string parameters i.e. ("SELECT [{0}] FROM [{1}] WHERE [Id] = {2};", c, t, id)
But it isn't as bad as it looks, I'm protected: Only authenticated users (i.e. signed in users) can execute the Page_Load I'm checking that both the table and the column exists beforehand (using GetSchema etc.)I'm checking that the Id is an integer beforehand All my tables have Id columns The database connection is reasonably secure
[Code]...
View 1 Replies
Mar 23, 2009
I have a requirement where I have to add items into a List(Of T) (let's call it Target) from an IEnumerable(Of T) (let's call it Source) using Target.AddRange() in VB.NET.
Target.AddRange(Source.TakeWhie(Function(X, Index) ?))
The ? part is a tricky condition that is something like: As long as the as yet unenumerated count is not equal to what is needed to fill the list to the minimum required then randomly decide if the current item should be taken, otherwise take the item.Somethig like...
Source.Count() - Index = _minimum_required - _curr_count_of_items_taken _
OrElse GetRandomNumberBetween1And100() <= _probability_this_item_is_taken
' _minimum_required and _probability_this_item_is_taken are constants
The confounding part is that _curr_count_of_items_taken needs to be incremented each time the TakeWhile statement is satisfied. How would I go about doing that? I'm also open to a solution that uses any other LINQ methods (Aggregate, Where, etc.) instead of TakeWhile.If all else fails then I will go back to using a good old for-loop =)
View 2 Replies
Feb 3, 2010
I'm working on my first project using LINQ (in mvc), so there is probably something very simple that I missed. However, a day of searching and experimenting has not turned up anything that works, hence the post.
I'm trying to write a LINQ query (Linq to SQL) that will contain a multiple number of conditions in the where statement separated by an OR or an AND. We don't know how many conditions are going to be in the query until runtime. This is for a search filter control, where the user can select multiple criteria to filter by.
select * from table
where table.col = 1
OR table.col = 2
OR table.col = 7
'number of other conditions
Before I would just construct the SQL query as a string while looping over all conditions. However, it seems like there should be a nice way of doing this in LINQ. I have tried looking using expression trees, but they seem a bit over my head for the moment. Another idea was to execute a lambda function inside the where statement, like so:
For Each value In values matchingRows = matchingRows.Where(Function(row) row.col = value)However, this only works for AND conditions. How do I do ORs?
View 3 Replies
Jun 29, 2010
I'm trying to translate some C# LINQ code into VB.NET and am stuck on how to declare an anonymous type in VB.NET.[code]How do you translate C#'s new { ... } syntax into VB.NET?
View 2 Replies
Apr 5, 2011
I have a GridView and on a row being deleted I trigger the GridView1_RowDeleting sub, but I receive an error "LINQ to Entities does not recognize the method 'System.Web.UI.WebControls.TableCell get_Item(Int32)' method, and this method cannot be translated into a store expression." Code is:
Private Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
' The deletion of the individual row is automatically handled by the GridView.
Dim dbDelete As New pbu_housingEntities
' Remove individual from the bed.
[code]....
The specific line erroring out is:
remove_bed.First.occupant = ""
View 2 Replies
Apr 29, 2010
i'm having troubles with a linq-to-sql statement and getting a left outer join to work. here is a sample of two tables i'm trying to join and my code that i've wrote.
Table1 Table2
f1 f2 f3 f4 fID f1 f2 f3 f4
val1 val2 val3 val4 1 val1 val2 val3 val4
[code]....
Instead I'm returning ALL values from table 1. I need to return all values from the left outer join where tbl2.fID is null.
View 1 Replies
Nov 26, 2010
I am trying to create a LINQ query statement that retrieve intersecting data from 3 datatables (dt, dt1, dt2). My first attempt to do so with the following LINQ query statement was successful: [code] However in my design, I would need the LINQ query statement to be dynamically generated, because I want a user to be able to retrieve intersecting data entries from VARIABLE number of datatables. In this case, the first option is not as good as the "INTERSECT" query operator, with which I can just easily connect each SQL query statement with a "INTERSECT" operator. The problem is that my second attempt using the "intersect" operator fails, and after many trials and errors I still can't get it to work. [code]
View 5 Replies
Feb 3, 2010
Basically, I am trying to write a LINQ to Objects statement where the relationship is a grandparent, parent, child relationship. (You could also call it a Master Detail relationship.)
Dim coverages As New List(Of Coverage)
Dim coverage As Coverage
For Each rl In oClaimsPolicy.RiskLocations
[code].....
View 1 Replies