I have a database table that holds parent and child records much like a Categories table. The ParentID field of this table holds the ID of that record's parent record...
My table columns are: SectionID, Title, Number, ParentID, Active
I only plan to allow my parent to child relationship go two levels deep. So I have a section and a sub section and that it.
I need to output this data into my MVC view page in an outline fashion like so...
Section 1
Sub-Section 1 of 1
Sub-Section 2 of 1
Sub-Section 3 of 1
Section 2
Sub-Section 1 of 2
Sub-Section 2 of 2
Sub-Section 3 of 2
Section 3
I am using Entity Framework 4.0 and MVC 2.0 and have never tried something like this with LINQ. I have a FK set up on the section table mapping the ParentID back to the SectionID hoping EF would create a complex "Section" type with the Sub-Sections as a property of type list of Sections but maybe I did not set things up correctly.
So I am guessing I can still get the end result using a LINQ query. Can someone point me to some sample code that could provide a solution or possibly a hint in the right direction?
Update:
I was able to straighten out my EDMX so that I can get the sub-sections for each section as a property of type list, but now I realize I need to sort the related entities.
var sections = from section in dataContext.Sections
where section.Active == true && section.ParentID == 0
orderby section.Number
select new Section
{
SectionID = section.SectionID,
Title = section.Title,
Number = section.Number,
ParentID = section.ParentID,
Timestamp = section.Timestamp,
Active = section.Active,
Children = section.Children.OrderBy(c => c.Number)
};
produces the following error.
Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Data.Objects.DataClasses.EntityCollection'
Your model has two navigation properties Sections1 and Section1. Rename the first one to Children and the second one to Parent.
Depending on whether you have a root Section or perhaps have each top-level section parented to itself (or instead make parent nullable?), your query might look something like:-
// assume top sections are ones where parent == self
var topSections = context.Sections.Where(section => section.ParentId == SectionId);
// now put them in order (might have multiple orderings depending on input, pick one)
topSections = topSections.OrderBy(section => section.Title);
// now get the children in order using an anonymous type for the projection
var result = topSections.Select(section => new {top = section, children = section.Children.OrderBy(child => child.Title)});
For some linq examples:
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
This covers pretty much all of the linq operations, have a look in particular at GroupBy. The key is to understand the input and output of each piece in order to orchestrate several in series and there is no shortcut but to learn what they do so you know what's at hand. Linq expressions are just combinations of these operations with some syntactic sugar.
Related
I have the below requirement to be implemented in a plugin code on an Entity say 'Entity A'-
Below is the data in 'Entity A'
Record 1 with field values
Price = 100
Quantity = 4
Record 2 with field values
Price = 200
Quantity = 2
I need to do 2 things
Add the values of the fields and update it in a new record
Store the Addition Formula in a different config entity
Example shown below -
Record 3
Price
Price Value = 300
Formula Value = 100 + 200
Quantity
Quantity Value = 6
Formula Value = 4 + 2
Entity A has a button named "Perform Addition" and once clicked this will trigger the plugin code.
Below is the code that i have tried -
AttributeList is the list of fields i need to perform sum on. All fields are decimal
Entity EntityA = new EntityA();
EntityA.Id = new Guid({"Guid String"});
var sourceEntityDataList = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities;
foreach (var value in AttributeList)
{
EntityA[value]= sourceEntityDataList.Sum(e => e.Contains(value) ? e.GetAttributeValue<Decimal>(value) : 0);
}
service.Update(EntityA);
I would like to know if there is a way through linq I can store the formula without looping?
and if not how can I achieve this?
Any help would be appreciated.
Here are some thoughts:
It's interesting that you're calculating values from multiple records and populating the result onto a sibling record rather than a parent record. This is different than a typical "rollup" calculation.
Dynamics uses the SQL sequential GUID generator to generate its ids. If you're generating GUIDs outside of Dynamics, you might want to look into leveraging the same logic.
Here's an example of how you might refactor your code with LINQ:
var target = new Entity("entitya", new Guid("guid"));
var entities = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities.ToList();
attributes.ForEach(a => target[a] = entities.Sum(e => e.GetAttributeValue<Decimal>(a));
service.Update(target);
The GetAttributeValue<Decimal>() method defaults to 0, so we can skip the Contains call.
As far as storing the formula on a config entities goes, if you're looking for the capability to store and use any formula, you'll need a full expression parser, along the lines of this calculator example.
Whether you'll be able to do the Reflection required in a sandboxed plugin is another question.
If, however, you have a few set formulas, you can code them all into the plugin and determine which to use at runtime based on the entities' properties and/or config data.
I hope one can help me, I am new in linq,
I have 2 tables name tblcart and tblorderdetail:
I just show some fields in these two tables to show whats my problem:
tblCart:
ID,
CartID,
Barcode,
and tblOrderDetail:
ID,
CartID,
IsCompleted
Barcode
when someone save an order, before he confirms his request,one row temporarily enter into the tblCart,
then if he or she confirms his request another row will be inserted into the tblOrderDetail ,
Now I wanna not to show the rows that is inserted into tblOrderDetailed(showing just temporarily rows which there is in tblCart),
In another words, if there is rows in tblCart with cartID=1 and at the same time there is the same row with CartID= 1 in tblOrderDetail, then I dont want that Row.
All in all, Just the rows that there isnt in tblOrderDetail, and the field to realize this is CartID,
I should mention that I make Iscompleted=true, and with that either we can exclude the rows we do not want,
I did this:
var cartItems = context.tblCarts
.Join(context.tblSiteOrderDetails,
w => w.CartID,
orderDetail => orderDetail.cartID,
(w,orderDetail) => new{w,orderDetail})
.Where(a=>a.orderDetail.cartID !=a.w.CartID)
.ToList()
however it doesn't work.
one example:
tblCart:
ID=1
CartID=1213
Barcode=4567
ID=2
CartID=1214
Barcode=4567
ID=3
CartID=1215
Barcode=6576
tblOrderDetail:
ID=2
CartID=1213
Barcode=4567
IsCompleted=true
with these data it should just show the last two Row in tblCart, I mean
ID=2
CartID=1214
Barcode=4567
ID=3
CartID=1215
Barcode=6576
This sounds like a case for WHERE NOT EXISTS in sql.
roughly translated this should be something like this in LINQ:
var cartItems = context.tblCarts.Where(crt => !context.tblSiteOrderDetails.Any(od => od.CartID == crt.cartID));
If you have a navigation property on cart to reference details (I'll assume it's called Details), then:
var results=context.tblCarts.Where(c=>!c.Details.Any(d=>d.IsCompleted));
I need a little help with Linq i have a DB table with restourants tables in it. In the DB i have "TableNumber, Floor , RestaurantID" I would like to get list of all floors. For example if i have this list:
TableNumber, Floor , RestaurantID
10 1 1
11 1 1
12 2 1
13 2 1
14 3 1
I would like to get only "1,2,3".
Right now the method returns all rows.
public IEnumerable<ListPad.Item> GetFloorsListPads(SSF.ART.Key restaurant_id)
{
return from restaurant_floor in EnterpriseTouchRestaurantApplication.TouchRestaurant.AllRestaurantTables
where restaurant_floor.RestaurantID == restaurant_id && restaurant_floor.Active == true
orderby restaurant_floor.Floor
select new ListPad.Item()
{
Color = Color.SkyBlue,
Text = string.Format("{0}", restaurant_floor.Floor),
Tag = restaurant_floor
};
}
Thanks for all the help in advance.
You need either one or two things, depending of whether or not ListPad.Item defines equality (by overriding Equals and GetHashCode) in the way you describe. If so, then adding .Distinct() to your query will give you the distinct items. If not, you can do it one of three ways.
Return an anonymous type, call Distinct on it, and map to the actual type (lazy way)
Implement IEquatable on ListPad.Item, overriding Equals and GetHashCode (you'll need to research how to properly implement GetHashCode so it matches your equality conditions)
Define an IEqualityComparer<ListPad.Item> that defines your equality.
1 is the lazy way but is less coding. 2 is handy if your conditions define equality for ALL uses of ListPad.Item (not just this particular scenario). 3 separates the equality check from the actual type, which is handy if you have other cases where equality would be defined differently.
I have a small application that displays a list of shortURLs to a user. I'm using ASP.NET MVC3, Entity Framework, and an Oracle backend. I'm also using the PagedList library by Troy Goode (https://github.com/TroyGoode/PagedList)
I want the user to see the very last entry first, similar to how blog comments are ordered. To do this I added an "orderby descending" clause to the LINQ statement:
var links = from l in db.LINKS
orderby l.ID descending
select l;
In debug the "links" object is ordered as expected (in descending order, by the primary key "ID").
Now I want to pass this list of links to the view using the .toPagedList() method:
int pageSize = 3;
int pageNumber = (page ?? 1); // "page" comes from the request, if null set to 1
return View(links.ToPagedList(pageNumber, pageSize));
This works great if I only have 3 records (refer to "pageSize" above). However, when I add a 4th record I get unexpected behavior. Because I have the pageSize set to 3, adding a fourth record means there will be 2 pages. I would expect the ordering to be as follows:
Page 1:
ID: 4
ID: 3
ID: 2
Page 2:
ID: 1
That is not the case, what actually happens is this:
Page 1:
ID: 3
ID: 2
ID: 1
Page 2:
ID: 4
So my question, what the heck am I doing wrong here? Why is PagedList not following the order defined by the "orderby l.ID descending" expression in the LINQ statement? It is baffling to me because in the debugger it is clear that the "links" object is ordered properly before the .toPagedLIst() is used on it.
Any help is greatly appreciated!
You could try:
return View(links.ToPagedList(pageNumber, pageSize, m => m.ID, true));
It's off the top of my head, so I'm sorry if it doesn't work perfectly...
-- Wow, just noticed the date on this one... I need to stop trawling the unanswered pages without sorting by date!
I have 3 tables: A, B and C.
Table A is in relation (n:1) with B and with C.
Typically I store in A the B.Id (or the C.Id) and the table name.
e.g.
A.ParentId = 1
A.TableName = "B"
A.ParentId = 1
A.TableName = "C"
A.ParentId = 2
A.TableName = "B"
Is it a good solution? Are there any other solutions?
Why not 2 parentid columns?
A.ParentIdB = 1
A.ParentIdC = 3
Another possibility is to introduce another table Content (D) that serves as a "supertype" to Posts and Images. Then a row in Comments (A) would reference a primary key in Content as would each row in Posts (B) and Images (D). Any common fields in Posts and Images would be moved to Content (perhaps "title" or "date") and those original tables would then only contain information specific to a post or image (perhaps "body" or "resolution"). This would make it easier to perform joins than having the table names in a field, but it does mean that a real-world entity could be both a post and a comment (or indeed, be multiply a post or comment!). Really, though, it depends on the situation that you're trying to model.