Linq To Entities Query With Child Objects?

Oct 13, 2011

Basically I have the follwing:Dim ctx As New AdminCoreEntities Dim roles = (From r In ctx.Roles where r.Name.StartsWith("cust") Select r) 'list of System .Linq.IQueryable(Of AdminCoreModel.Role)

[code]...

The error i get when i run this is: Unable to cast object of type System.Data.Objects.ObjectQuery`1[AdminCoreModel.Role] to type AdminCoreModel.Role

Basically I have a many to many situation and I try to get all the Quicklinks objects queried by their roles and not quite sure why EF will cast to a single AdminCoreModel.Role when i.Roles is a collections of objects.

View 1 Replies


ADVERTISEMENT

Filter Parent / Child Table In Linq Query Based On Entities In Child Table?

Jul 9, 2010

I have a table (Projects) which is linked to projectVersions on projectID..projectVersions contains several columns on which I'd like to filter a returned project (and associated projectVersions) list. For example there is a "capacity" column and a "country" column. I am doing a filtered list of projects on one page and I'd like to include all projects where any one of the associated projectVersions has a capacity of 750ml and a country of "France" for example.It may be that a particular parameter is not set and so I pass a zero to indicate not to filter on that.I guess this needs some kind of subquery as when I try and do something like this: [code] it doesn't work as the "xxx" bit, being one-to-many, expects me to specify an item to get at the entities within and yet I want to query where ANY of the associated versions match one of the criteria.

View 1 Replies

EF4: LINQ 2 Entities Query Works In C# But Not In VB

Dec 11, 2010

the query below throws a System.NotSupportedException: "Unable to cast the type 'System.Linq.IOrderedQueryable1' to type 'System.Linq.IQueryable1'. LINQ to Entities only supports casting Entity Data Model primitive types." The exception is only raised in the VB.Net version. When translated to C#, no exception is raised.

Dim doesThisCrash = From outerOrder In orders
Where outerOrder.ProductId =
(From p In products Join o In orders On p.Id Equals o.ProductId

[Code]....

View 4 Replies

.net - Detect Empty Results In A Linq-To-Entities Query?

May 29, 2009

The only way I know of is awkward:

'check for empty return
Dim count As Integer = (From s In myEntity.employee Where employeeID = myEmployeeIDVariable).Count
'If there is a record, then process

[code]....

View 2 Replies

.net - In Linq To Entities - How To Do An Inner EXISTS (SELECT 1 FROM - Type Of Query

Mar 6, 2011

I am trying to write a LINQ equivalent of

SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
EXISTS (SELECT 1 FROM T2
WHERE T1.C17 = T2.C24)
ORDER BY C3

I'm using EF CTP 5, so I have a DBContext variable named dbc, which includes DBSet objects T1s and T2s, based on POCOs T1 and T2.

In LINQ I write

DIM IND = From i In dbc.T1s
Where i.C4 = "xyz"
And (From t In dbc.T2s Where i.C17 = t.C24).Any
Select i.C1, i.C2, i.C3
Order By C3

Running the query I get the error message "Unable to create a constant value of type 'T2'. Only primitive types ('such as Int32, String, and Guid') are supported in this context." When I omit the inner expression (third line in the LINQ code), the query runs fine. I tried switching the orders of the inner comparison, to be t.C24 = i.C17, with no effect.

View 1 Replies

Update One Entity Based On Results Of Another LINQ-to-Entities Query

Apr 9, 2011

I have the following LINQ-to-Entities query:[code]This will give me all the resident assignments for the current year/term. Then I have this LINQ-to-Entities query:[code]This will give me all the rooms. I want to iterate through the assignments and based on what room is assigned update the occupancy in reset_occupancy. I'm not 100% sure how to accomplish this. Here is my pseudo code of what I want to accomplish:[code]

View 1 Replies

Update One Entity Based On Results Of Another LINQ-to-Entities Query?

Dec 15, 2009

I have the following LINQ-to-Entities query:

' Get all the residency assignments that match the term/year.
Dim assignments = From p In dbContext.Residents _
Where p.semester = term _

[code].....

View 1 Replies

LINQ To XML And Child Objects?

Nov 20, 2009

I am creating a tool that interfaces with an API for a CD/DVD burner. I have created several custom classes that will hold the information for the disk sets (which consist of a data path and a label file among other information. The DiskSet class has a list of Include objects. My proposed XML output would be something similar to the following:

<DiskSets>
<DiskSet>
<Name>MySet 1</Name>

[code].....

I am attempting to traverse the Includes collection (I have tried List and Binding List as I am also using this in a pair of datagridview controls) and it keeps returning Expression of type 'Object is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider." How can I use LINQ to pull a list of the Include objects under each DiskSet object for transformation into XML? I am hoping to keep this restricted to this one block of code if possible.

View 7 Replies

Creating Objects From Child Elements Using LINQ To XML

Feb 1, 2011

Is there any way to use a LINQ to XML to query an XML document like the one below to create new (anonymous or strongly typed) objects from the child elements of a descendant? Here is my XML document:

[Code]...

View 2 Replies

LINQ To Objects, Query Arrays In Object?

Sep 15, 2011

I need to query an array. I have tried to use LINQ but with no success.

Here is a simplified structure of Insurance

Public Class Insurance
Public _typ As String
Public _numb As Number()

[Code]....

View 1 Replies

Order By And Group By In A Linq To Objects Query?

Sep 28, 2010

How do I order by and group by in a Linq query?I tried..Dim iPerson = From lqPersons In objPersons Where Len(lqPersons.Person) > 0 Group lqPersons By key = lqPersons.Name Into Group Order By Group descending Select Group, key

For Each i In iPerson
tmp = tmp & vbNewLine & i.key & ", " & i.Group.Count
Next

[code].....

View 5 Replies

.net - LINQ Query Returns List Of Empty Objects?

May 3, 2012

I have a query that 'selects' object of a type:

Dim l as IList(Of Foo) = (From dataRow As DataRow In table.Select()
Where CStr(dataRow("Column1")) = "A"
Select New Foo(CStr(dataRow("Column1")), _
CStr(dataRow("Column2")))).ToList()

What's happening is that if i set a break-point to the constructor of Foo and step, the constructor is hit and the parameters are loaded with the arguments. However, l has empty Foo objects (the members in every object are Nothing). What could be happening here?

View 1 Replies

Linq To Xml Query Returning A List Of All Child Elements?

Mar 8, 2010

I have got an xml document which looks something like this.

<Root>
<Info>
</Info>
<Info>

[Code]...

How can i write a linqToXML query so that it returns me an IEnumerable containing each child element, in this case all five child elements of , so that i could iterate over them. The order of child elements is not definite, neither is number of times the may appear.

View 1 Replies

Array Of Child Objects In Nested Classes And Accessing Child Objects In .Net

Jan 3, 2012

I have a nested class, let's call it class1 and it has class2 inside it; VB.Net eg:[code]

1) How can I define X number of Class2 objects[let's call it: Node(x) array]** with NEW() subroutine called?' this raises error: dim cls2(n) as new class2 end sub.

2) How can I return actual number of Node() array? [code]Outside my class in main project I define cls1 object:[code]Now an array of class2 is created inside cls1.

3) Now,How can I access All of them[node(x) array which is created inside cls1] with all properties and methods available?

I remember I wrote a ProcessManager class with this functionality in .net 2003, nearly 4 years age, I don't have the code now.

View 1 Replies

VS 2010 Dynamically Building LINQ Query To Search Objects

Mar 24, 2011

I am creating a mediaplayer / playlist creator tool that basically imports MP3 songs into playlists (grids). I'm looking to add search functionality where the user can search all open playlists (or a subset of the open playlists). A Playlist is basically a collection of AudioFiles, where an AudioFile is a class that holds the filename of the MP3 file and then exposes the tags (Title, Artist, Album, etc) in public properties.

The idea is that the user can search for a certain expression (for example "the"), and that he can then choose whether to search in the title, in the title and artist, in the title or artist, in the title, artist and/or album, etc. However, I'm having a hard time implementing this and also having it kind of dynamic.

At the moment I am simply gathering all the AudioFiles from the open playlists (or from the subset of playlists the user chooses) and then running a simple LINQ query to find those AudioFiles where the Title contains the search text. Then I display the resulting AudioFiles in a new playlist window:

[Code]....

In case the AND / OR distinguishing is too hard, I would be happy to start with just letting it default to 'OR', so that the user can check Title and Album for example. If he would then search for 'a' then all songs that contain 'a' in their title as well as all songs that contain 'a' in their album should be returned. I guess that makes it easier, but I still wouldn't know how to dynamically create the LINQ query.

View 2 Replies

Linq To Entities. GetPropety(name).GetValue Not Recognised By Linq?

Apr 20, 2011

I have a listbox on my xaml form that I bound to a List(Of MyType) property. I populated this list like so:

Dim fields As List(Of CheckableFields) = New List(Of CheckableFields)
Using context As ITIPEntities = New ITIPEntities()
Try[code]....

Now I'm at the point where the user selects the fields they want included in a report and I need to iterate over the required fields. This is my linq query:

For Each checkedField In _requiredFields
If checkedField.IsChecked Then
If checkedField.FieldData IsNot Nothing AndAlso checkedField.FieldData.Trim IsNot String.Empty Then[code].....

View 1 Replies

Write A LINQ To Objects Statement Where The Relationship Is A Grandparent, Parent, Child Relationship?

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

Abstraction Between Database Entities And Objects In C#?

Sep 17, 2011

I've just gotten started with development using Visual Studio (though this can be seen as a more language-generic question), and need done design answers regarding how to suitable relate a Database Entity/Table with an Class in-code.

Currently, I create a class who's attributes or iVars directly correspond to the fields in an Entity.

When I come to fetching all 'Customers from a Table' for example, I run a Select all statement against the database and for each result-row returned, I instantiate a new class Object where I use the Class' default constructor to set the instance variables. (I really wish there was a method like PHP's mysql-fetch-object()?)

I can then add each of these objects to say, a List(of Customer) and return it.

This doesn't seem like the best way to do it. If I were to change the Database schema, I'd have to reflect those changes in the class also.

View 1 Replies

.net - Linq Expressions With Linq To Entities?

Feb 28, 2012

The pattern I'm trying to avoid is checking if a string (normally a control's text value) is null/empty, and if it is, comparing it using Contains to a field in my data. Obviously the field isn't hard-coded into my extension, neither is the object type.What I've got works perfectly in Linq to Objects, but I get the generic run-time error "LINQ to Entities does not recognize the method 'System.String Invoke(GenericQueryHelper.Customer)' method, and this method cannot be translated into a store expression." when using an entity framework model.

Here's what I have:

<System.Runtime.CompilerServices.Extension()>
Public Function CompareAndFilter(Of T)(source As System.Linq.IQueryable(Of T), expressionField As System.Linq.Expressions.Expression(Of System.Func(Of T, String)), compareTo As String)

[code]....

I want my usage to look something like this:

Dim results = repository.Customers.CompareAndFilter(Function(c) c.FirstName, searchText)

I do need to get this running against a SQL database really, as it is filtering results, so I don't want to be doing that in memory.

View 2 Replies

OOP - Objects For Entities With Master Lists And Object Composition?

Feb 23, 2009

I'm trying to wrap my head about how to properly implement an OOP design for business objects that:

Have a "master list" in a database (ex. classifications)Are a part of another object as a property (i.e. object composition) but with additional properties

Here is where I'm stuck on the theory. Suppose that I have a Classification object, inheriting from the abstract class BusinessObject with the CRUD functions defined (MustOverride). This will give me:

[Code]...

What kind of other design could I implement here? It's obvous that I'm designing this the wrong way, but I don't see an easy alternative pattern. I don't want to repeat the code in the CustomerClassification class by not inheriting Classification and repeating all the field and property code, but I also don't want to expost CRUD functions to the CustomerClassification level. What am I missing in looking at the overall class design?

EDIT: Saving the classification level to the database will be handled by the customer object, because the database is legacy and the field for Level is defined in the customer table.

View 2 Replies

Inner Join In Linq To Entities?

Apr 23, 2009

Given two tables, customer and orders, how would one do a linq query to entities to find any customers with open invoices started before a certain date?

View 1 Replies

Asp.net - Concatenating String In LINQ-to-Entities?

Apr 7, 2011

This code works if I take out

" " + p.MIDDLE_NAME

Thus the remaining code looks like:

p.FIRST_NAME + " " + p.LAST_NAME

Maybe LINQ-to-Entities just doesn't support concatenating more than two strings at a time?

Protected Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click
Dim dbContext As Campus6Entities = New Campus6Entities

[Code]....

View 1 Replies

Databind To A Treeview Using Linq To Entities?

Feb 12, 2010

I have seen other similar questions dealing with WPF Almost exactly the same but I can;tfigure this out in ASP.net. I have a Pages table with a parentpage foreign key and want to databind them to a treeview. In the past I have created a hierarchicaldataset from a dataset but would like to stay within the entities framework if I can

View 1 Replies

Get CurrentTime From SQL Server With LINQ/Entities?

Sep 14, 2011

I am using LINQ with Entity Framework, and I need to get the SQL server timeCan anyone tell me how can I create a method so it retrieves the current server time?

View 1 Replies

VS 2008 Linq 2 Entities Can't Add An Association

Mar 21, 2009

I'm a bit of a n0ob to all this and I'm having trouble finding good resources on the net.What I'm trying to do is define a one-to-many relationship between two entities. I'm using the GUI tool in Visual Studio - and have created the tables in Microsoft SQL Server Management Studio.Basically, I have two tables....a 'Users' table and a 'Sites' table. A single user can have many sites associated with it.Users has a primary key of 'UID' and the Sites table as a primary key of 'Site_ID'.Sites also has a column 'UID' that should indicate which user that site is associated with.Anyway - it sounded pretty simple; but no matter what I attempt to do, I can't seem to get it to work. Mostly, I get validation errors - sometimes run-time errors, or sometimes, no actual data even though there should sites that match the UID of the user.I am able to connect and write LINQ against my Users table/pull down User objects - but can't seem to get it to know that the Sites are associated with the user.In the GUI - if I go to 'Add Association' and create a 1 to many link between Accounts and Sites - and Validate - I get an error saying, "Error1Error 11008: Association 'AccountsSites' is not mapped."

It seems like, when I try to make an association, it wants to link the 'Keys' of each entity.But U_ID and SITE_ID are the keys and, obviously, shouldn't match.If I click on the line it created in the GUI and I go to 'Table Mapping' I can see that it is trying to match those keys. Since I want it to match user_ids my first thought is to just make the U_ID the entity Key in the Sites entity but, of course, that's wrong because it would no longer allow a 1 to many relationship.

View 1 Replies

Linq To Entities, Filter Using Where And A List Of Stings?

Nov 2, 2011

I am trying to write a query for an existing database, I am using LINQ on EF3.5 inside VS2008 as that is the only environment available to me.I have a starter query that gives me a list of items so the user can select what they want.

"e.Result =
From c In MLDb.TBL_FeatureInfoSet
Order By c.INSP_ROUTINE_NM
Select c.INSP_ROUTINE_NM
Distinct"

The user can then select from the list one or several items they are intersted in and i want to use these as the filter in a further database call. It's not then difficult to have a where to filter on one string however, I would also like to be able to allow the user to select several items from the list box and the data for all of those to be returned.

[Code]...

View 2 Replies

Update An Object's Foreign Key Value With LINQ To Entities

May 4, 2012

[code]How do I update the ProdServiceID in the database using LINQ to entities?

View 1 Replies

Asp.net - LINQ To Entities Does Not Recognize The Method System.ObjectCompareObjectEqual

Mar 21, 2012

I had a code segment on an asp.net page in the code-behind file. I decided to move it into a public subroutine in a module (general_functions.vb). Once I did this, however, the code no longer works - it throws an error. On the original code-behind I replaced the original code with a call like so:

[Code]...

View 1 Replies

LINQ To Entities .Any() Results In Null Reference Exception?

Nov 16, 2011

I have a simple query as below:

Dim sizings = From a In db.Sizings
Where a.Customer.ID = customer.ID
Select a
If sizings.Any Then
.....

The sizings.Any line is throwing a null reference exception. I thought I was meant to use .Any to determine if there were any rows returned?isnothing(sizings) returns false.

Edit - Resolution:Don't use null objects in the LINQ Query!

View 2 Replies

Use Linq To Entities To Group A Collection Without Using Anonymous Types?

Apr 24, 2012

The documentation has an example about grouping on multiple properties[code]...

Is it possible to rewrite the original query to just return IEnumerable(Of CustomerRegionGroup), or do I have to use the anonymous type, and run a second query on the result of the first?

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved