I'm getting an Entity Framework object collection from my Products table called oProducts. I am looping through the collection and setting the Quantity field from a separate function. Before I write out a display of the objects in HTML, I want to sort them by the Quantity. So, I am using LINQ to create a new collection and try to order from the object collection that I just modified. The modified values are definitely in the object collection and output properly, but the sort ordering isn't working on the modified field. Does anyone know what I'm overlooking or a better way to attempt this?
Dim oProducts = From p in ctx.Products _
Where p.Active = True _
Select p
For Each prod in oProducts
prod.Quantity = GetQuantity(prod.ProductID)
Next
Dim oOrderedProducts = From p in oProducts _
Order By p.Quantity Ascending _
Select p
For Each prod in oOrderedProducts
Response.Write(prod.Quantity.ToString())
'*** The Quantities are stored in the collection properly but it doesn't order by the Quantity field.
Next
There a few things you need to remember:
Doing From p in ctx.Products _ will always select from the database.
LINQ is lazy. It will not perform the database select until you iterate, and if you iterate twice, it will do the database select twice.
How data read from the database is combined with data already in the ObjectContext will vary based on the MergeOption of the query you execute. But it will always happen after the SQL is executed.
With those ideas in mind, let's consider your code:
This bit (1) creates an IQueryable<Product> which can be used as a basis for iteration or defining other queries. Note that it does not actually execute a database query.
Dim oProducts = From p in ctx.Products _ // (1)
Where p.Active = True _
Select p
The next bit (2) iterates the IQueryable defined above.
For Each prod in oProducts // (2)
prod.Quantity = GetQuantity(prod.ProductID)
Next
Doing so causes a selection from the database. Inside the loop, you mutate the object that's returned. In LINQ to Objects, this would have no effect whatsoever, because the next time you iterated the IQueryable it would be executed anew. However, Entity objects are special. Mutating them marks the entity is changed on the context. Calling SaveChanges() later would save those modifications to the database.
It's important to note, however, that you have not changed the IQueryable variable oProducts at all by doing this. It is still a reference to a query which will be executed every time it is iterated. It is not a list. If you want a list, you need to call ToList() or ToArray().
The following query (3) creates a new IQueryable based on the original oProducts. When iterated, this will produce new SQL which does much the same as the original query, except for the ordering you've added. Again, oProducts is a query, not a list.
Dim oOrderedProducts = From p in oProducts _ // 3
Order By p.Quantity Ascending _
Select p
Finally, your code executes a new database query based on the IQueryable you defined above. As it executes this query, it merges the information retrieved from the database with the knowledge about the mutation you did above from the object context. Because the ordering is based on the query you defined, it is performed on the database server, using information in the database. Because information from the database is merged with information from the object context, you still see the mutation you performed on the data above, in (2).
For Each prod in oOrderedProducts
Response.Write(prod.Quantity.ToString())
'*** The Quantities are stored in the collection properly but it doesn't order by the Quantity field.
Next
You don't need to use the 3 consecutive LINQ expressions, combine them into one, using projection, either into an anonymous type (as I did here) or a real one.
This, below, should work
var oProducts = From p in ctx.Products
Where p.Active == True
orderby p.Quantity Ascending
select new
{
Quantity = GetQuantity(p.ProductID)
// ... assign other properties too
}
foreach ( var prod in oProducts )
{
// do your output
}
How about this?
Dim oProducts = From p in ctx.Products _
Where p.Active = True _
Select p
For Each prod in oProducts
prod.Quantity = GetQuantity(prod.ProductID)
Next
Dim oOrderedProducts = oProducts.OrderBy(Function(p) p.Quantity)
Related
Is there a way to increase the fetching performance of my LINQ to SQL query where its total count is more than a hundred thousand? Should I separate the total data to 5 parts by using skip or take? My query, when fetching, took more than 40 minutes.
Dim query = From a In context.Orders
Join b In context.Status On a.OrderItem Equals b.OrderItem
Join c In context.Summary On a.OrderItem.Substring(0, 16) Equals c.OrderSuffix
Where Not (b.Status.Contains("E")) And a.Type = "AG" And a.OrderItem = b.OrderItem And a.OrderItem.Substring(0, 16) = c.OrderSuffix
Order By a.OrderItem.Substring(0, 16), a.Agn Ascending
Select a.OrderItem,
JobOr = a.auftrag_nr.Substring(0, 16),
Suffix = (a.auftrag_nr.Substring(0, 16).Substring(13)),
a.Agn,
a.Base,
b.Status,
a.Group,
a.Machine,
a.Article,
a.Height,
a.Sol, c.plan, a.DatePlanned
Actually you make a really big join, which you filter afterwards.
If you have a filter like a.type = "AG" than you should apply it before you join, so many unnecessary join results will not be even generated.
Another idea for you:
The other style of writing linq queries.
like:
dim foo as IQueryable(Of TypeInYourTable) = yourDBContext.yourTable.where(function(v) v.a = ...).SomeOtherLinqFunctions...
In this way you can build up the queriables for each table and filter them first (e.g. the a.type = "AG"), then you can use the .Join(...) function and make proper joins, where you not just compare one pair of values of two datasets.
Be aware, that as long as the Queryable is a Queryable, no call was made to the database. You need to call e.g. .ToArray() or .ToList() or ... to enforce the call.
If you don't and you return a Queryable, then close the context of it and then try to get the results of the Queryable you will get a runtime exception.
I have an API that accepts a list of objects(Object contains ProductID and Quantity). I want to determine whether there are duplicate product ID on the passed list. If duplicates are found, I'll merge them by adding the Quantity of duplicate product ID. To do this, I created my own algorithm that loops the list one-by-one then store it to another list (the verified one). The code goes like this.
For Each passedVoucher As VoucherInfo In VouchersToRelease
indexofduplicate = 0
sumQuantity = 0
hasDuplicate = False
If VerifiedReleasedVouchers.Count() > 0 Then
'for loop that checks if productID exists in the VerifiedReleasedVouchers
For Each finalList As VoucherInfo In VerifiedReleasedVouchers
If passedVoucher.ProductID = finalList.ProductID Then
indexofduplicate = VerifiedReleasedVouchers.IndexOf(finalList)
'if true, adds the Quantity of duplicate to the existing quantity at the VerifiedReleasedVouchers
sumQuantity = Convert.ToInt32(VerifiedReleasedVouchers(indexofduplicate).Quantity) + Convert.ToInt32(passedVoucher.Quantity)
VerifiedReleasedVouchers(indexofduplicate).Quantity = sumQuantity.ToString
hasDuplicate = True
Exit For
End If
Next
End If
'adds the voucher to verified released voucher if no duplicate was found
If hasDuplicate = False Then
VerifiedReleasedVouchers.Add(passedVoucher)
End If
Next
So what I did is, I ForEach looped the passed list. Inside the loop, I compared the current object from the passedList into every object in the verifiedList(w/c is empty by default) to determine whether their are duplicate product ID. If no duplicate was found, i'll just add the current object to the verifiedList. If duplicate was found, ill just update the object from the verified list with the same ProductID by storing the sum of the Quantity of both objects.
The code above works perfectly as intended but the thing is, it performs a lot of tasks. Is there any way to simplify what I did above?
P.S. List.Distinct() is not a solution for this
You could use Linq to easily achieve what you want. First you must GroupBy ProductID and then Select all the groups with a sum of the quantity. Finally, we get a list:
Dim VerifiedReleasedVouchers As List(Of VoucherInfo) = VouchersToRelease.GroupBy(Function(x) x.ProductID).[Select](Function(y) New VoucherInfo() With {
.ProductID = y.Key,
.Quantity = y.Sum(Function(z) z.Quantity),
.Denom = y.First().Denom
}).ToList()
Step 1: I am using entity to get the result. I run a query like this
Dim inbox = From p In dbContext.Inboxes Where p.RecordId = member_id Select p
Step 2: After that I put that into
Dim inboxList As IEnumerable(Of Entities.Inbox) = inbox.ToList()
so somewhere between step 1 and 2 I need to sort the List like so
inbox.OrderByDescending(Function(p) p.Importance) <-- PROBLEM HERE
The above line seems to have results, no exceptions, it just does not do anything!?
Any advice?
Order by will return a re-arranged value it won't actually rearrange itself.
You want:
inbox = inbox.OrderByDescending(Function(p) p.Importance)
Without assigning an output to a variable, it won't do anything
So I have an Entity Framework 5 model that includes a many-to-many relationship.
CategoryValues --< CourseCategoryValues >-- Courses
I have a LINQ query that selects every Course in the database. I would really like to modify it to only select Courses that belong to a specific CategoryValue. My attempt thus far has failed?
Can anyone help me figure this out?
This is what I have tried:
Using database As SiteDataContext = New SiteDataContext
database.Configuration.ProxyCreationEnabled = False
database.Courses.Include("Classes")
database.Courses.Include("CourseCategoryValues")
query = (From c In database.Courses Select c Order By c.Name).Where(
Function(c) 0 < c.Classes.Where(Function([class]) [class].Status.ToLower = "open").Count
).Include(Function(r) r.Classes).Include(Function(r) r.CourseCategoryValues)
' Here is where I am trying to narrow down the query results
If (pid.HasValue) AndAlso (0 <> pid.Value) Then
query.Where(Function(c) c.CourseCategoryValues.Any(Function(v) v.CategoryValue.CategoryValueID = pid))
End If
model.PageData = query.ToList
End Using
I think you are only missing the assignment of the filter to the query variable. Where returns a new queryable, it doesn't modify the queryable you apply the Where to. So, you would need:
query = query.Where(...)
The Where expression itself looks correct to me.
I have a the following setup
m_handsets = From p In RL.App.TComEntities.tblTelephoneNumbers _
Where p.companyId = m_CompanyID _
Select p
m_handsets = DirectCast(m_handsets, ObjectQuery(Of RL.TelephoneNumbers)).Include("tblCalls")
where m_handsets is defined as
Private m_handsets As IQueryable(Of RL.tblTelephoneNumbers)
which works as expected however what I want to do know is query the Navigation property (tblCalls) so I can do something like the following
From p In m_handsets.tblCalls
Where m_handsets.tblCalls.price > 100
but I have no idea of the proper syntax, can anyone help?
EDIT:
I think the complexity comes here because in this instance I could have 5 tblTelephoneNumbers in m_handsets and then x amount of calls for that particular telephone number. I am interested in the tblCalls for each but I would like to filter them all for each tblTelehoneNumber.
Entity Diagram which (hopefully) should illustrate further
So I currently know all the handsets that are associated with the company I am interested in. I can also see the calls loaded as a navigation property in debug mode, but I want to say is take this filter (in this example price>100 and apply it to all handsets->calls
If understand your question correctly, then you have 2 solutions to accomplish this:
In both solution you'll see that I remove the Include method since Include does NOT allow you to filter the related data.
1. Filtered Projection (Returns Anonaymous Type):
Dim results = From p In RL.App.TComEntities.tblTelephoneNumbers _
Where p.companyId = m_CompanyID _
Select New With {.Handsets = p, _
.tblCalls = p.tblCalls.Where(Function(t) t.price > 100)}
However, it might not be desirable in all situations as it gives a collection of anonymous type objects.
2. Two Tracked Queries (Returns EntityObjects):
This one gives you a collection of your entityobject tblTelephoneNumbers:
Dim m_handsets = (From p In RL.App.TComEntities.tblTelephoneNumbers _
Where p.companyId = m_CompanyID Select p).ToList()
Dim m_tblCalls = (From t In RL.App.TComEntities.tblCalls _
Where t.price > 100 Select t).ToList();
ForEach(Dim t In m_tblCalls)
m_handsets.Single(Function(h) h.ID = t.tblTelephoneNumberID).tblCalls.Add(t)
End ForEach
3. Leveraging Attach Method (Returns EntityObjects):
The last and probably the best and most elegant solution is to use EntityCollection.Attach Method along with EntityCollection.CreateSourceQuery:
foreach (var tel in m_handsets) {
IQueryable<tblCalls> sourceQuery = tel.tblCalls.CreateSourceQuery()
.Where(c => c.price > 100);
tel.tblCalls.Attach(sourceQuery);
}
Sorry for any VB syntax mistake, I wrote them all off the top of my head.