How To Query Navigation Properites Using Linq To Sql And Ef
Oct 4, 2010
I am trying to strongly type a query for 3 ef objects using linq to sql. There are one-to-many relationships with product and category. My classes contain navigation properties and look like this.
Currently I am trying to figure out how I can add dynamic query string parameters to my sitemap navigation menu. For example, the user chooses the source and edition he wants to work with. I have a simple sitemap that creates navigational links but the parameters the user chose need to be passed in the query string. The default map looks like this:
I've build a linq query.But i want a random selection so its not always ID : 1,2,3,4,5,6 How can i randomize this var? I like to bind it to a repeater.//TagCloud:
Random rand = new Random(); var tc1 = from i in JumpTide.cms.menu.GetMenuItems(32) select new
I am facing a big problem with simple linq query.. I am using EF 4.0..
I am trying to take all the records from a table using a linq query:
var result = context.tablename.select(x=>x);
This results in less rows than the normal sql query which is select * from tablename;
This table has more than 5 tables as child objects (foreign key relations: one to one and one to many etc)..
This result variable after executing that linq statement returns records with all child object values without doing a include statement.. I don't know is it a default behavior of EF 4.0 . I tried this statement in linqpad also..but there is no use... But interesting thing is if I do a join on the same table with another one table is working same is sql inner join and count is same..but I don't know why is it acting differently with that table only.. Is it doing inner joins with all child tables before returning the all records of that parent table?
So the BlogPostTags table only contains 2 fields, BlogPostID and TagID.
When i run the query above i get 3 results back. Same blogpost 3 times but with 1 tag in each. It should return 1 post with 3 tags. The problem lies in the Tags query above.
Is it possible to someshow select "child" controls of an <asp:Table> control so that I can view and set properties of the "child" control while in designer view? Its a real pain to have to edit properties of a child control in the source view. What I ultimately seem to end up doing is having to place or move any controls inside an asp:Table that I want to set properties on to the "outside" of the table and set all the properties on the control. After all the properties are on set on the control, using the designer,I can move it back inside the asp:Table. This is not the most troublesome of options but it would be nice if you could somehow set the properties of child controls using the designer.
first iam new here, and i have a little bit problems which i couldnt solve myself and i wish you will be my answer. ok so in my web i am using a master page and i want from another page to change the li html's elements on all its properties. how can i do that?
I have a web app for our golf club. When I compute handicap index for each golfer, I have to select the most recent scores and then a subset of those scores depending on how many rounds of golf the golfer has played. All the scores are entered into a single SQL Express table called "Rounds". Verbally, this is what I'm trying to do:
1) select the twenty most recent golf scores (sort on date descending, "take(20)") [if less than 20 records, then select all available];
2) for this set of records, select the 10 lowest scores (or smaller number if golfer has less than 20 rounds);
3) compute the average round differential for the subset of records, etc. to calculate handicap index (this step is working ok...)
My current VB code has this LINQ query (which is flawed -- it selects the lowest handicap differential scores of ALL records for the filtered user):
[Code]....
How do I modify this query to accomplish items 1) & 2) above? It seems this should be simple, but my experience with queries is still limited.
List<string> EmailId= { ....} Now I have a db table Users. I need to select all users whose email exist in the above list. In sql we could write "where emailid in ('email1', email2' ,...)"
how to do this in linq to sqlquerable<Users> existingUsers = Users.getTable().where (u=>u.emailaddress in EmailId). I want to do some thing similar
I need to figure out how to express this in linq to sql:I have one or more records that contain what is essentially a wildcard sql parameter, like '100_-___-2', which should match 100[any 1 char]-[any 1 char][any 1 char][any 1 char]-2, for example. Each login will have one or more of these 'datamasks' associated with it.I need to write a query in linq to sql that does something like:
select * from something where fieldA = 'someValue' or fieldB = 'someValue'
so far, simple, but I also need to restrict the returned records to only those that match the user's 'datamasks'. I'm not sure exactly how to translate this to linq to sql... here is what a working query *without* the datamasks part looks like:
[Code]....
I need to tack on the datamasks part... can I do it right there in the same statement? or do I need to do a foreach loop on the datamasks records and append a new && condition for each one? hmmm... just thought of that as I was writing this.. I'll try that out. But I'm posting this anyway for more input... I guess that would look something like:
I need to use Union for below Linq queries. I could use Union it if it was simple "select new whith{" but Now that I Specified Class, I encounter Error using union
Table User (UserID, Username) Table Admin (AdminID, Username) Table PM (PMID, SenderID, Sendertype, RecipientID, RecipientType) Get all Information from PM if SenderType == 'A' join SenderID to Admin-Table, get Username if SenderType == 'U' join SenderID to User-Table, get Username if RecipientType == 'A' join RecipientID to Admin-Table, get Username if RecipientType == 'U' join RecipientID to User-Table, Get Username
I have a datakey that I'd like to query from my GridView instead of looping through all the rows and comparing the key of each row. So I was wondering if it was possible to just do a linq query on the gridview (not datatable) and filter with the datakey.
I need to filter LINQ query using comboboxes and textboxes. The problem is I can't manage to get the result and I always get the empty gridview (used for showing filtered data). Can anyone help me why am I getting no results at all? I've checked the debugger and the data sent to query is valid, although, I'm not sure about "string.Empty" value.
SELECT Sum(ABS([Minimum Installment])) AS SumOfMonthlyPayments FROM tblAccount INNER JOIN tblAccountOwner ON tblAccount.[Creditor Registry ID] = tblAccountOwner. [Creditor Registry ID] AND tblAccount.[Account No] = tblAccountOwner.[Account No] WHERE (tblAccountOwner.[Account Owner Registry ID] = 731752693037116688) AND (tblAccount.[Account Type] NOT IN ('CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04')) AND (DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4 OR tblAccount.[State Change Date] IS NULL) AND ((tblAccount.[Account Type] IN ('CL10','CL11','PL10','PL11')) OR CONTAINS(tblAccount.[Account Type], 'Mortgage')) AND (tblAccount.[Account Status ID] <> 999) [code]....
MGEntities db = new MGEntities(); [HttpPost] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email); if (createStatus == MembershipCreateStatus.Success) { FormsService.SignIn(model.UserName, false /* createPersistentCookie */); MembershipUser myObject = Membership.GetUser(); Guid UserID = (Guid)myObject.ProviderUserKey; MyProfile profile = new MyProfile(); profile.Address = model.Address; profile.City = model.City; profile.Zip = model.Zip; profile.State = model.State; profile.UserId = UserID; Debug.Write(profile.State); db.aspnet_Profiles.Add(profile); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus)); } }
This is my MyProfile Class:
namespace MatchGaming.Models { [Bind(Exclude = "ProfileId")] public class MyProfile { [Key] [ScaffoldColumn(false)] public int ProfileId { get; set; } public Guid UserId { get; set; } [DisplayName("Address")] public string Address { get; set; } [DisplayName("City")] public string City { get; set; } [DisplayName("Zip")] public string Zip { get; set; } [DisplayName("State")] public string State { get; set; } } }
After the linq query is executed, i check my database and nothing is added. I am using POCO for my entities. Here is my class:
namespace MatchGaming.Models { public class MGEntities : DbContext { public DbSet<MyProfile> aspnet_Profiles { get; set; } } }
SELECT COUNT(AdvisorID) AS AdvisorRegisterLast2Days FROM ob_Advisor WHERE CONVERT(varchar,CreationDate,101) BETWEEN CONVERT(varchar,DATEADD(day,-4,DATEADD(day,2,GETDATE())),101) AND CONVERT(varchar,GETDATE(),101)
The problem is that I have an extra property on the Trip object that needs to be assigned, preferably without iterating through the returned IQueryable.
Does anyone know how to set the value during the query? (i.e. select t, t.property = "value")
I've been developing a ASP.NET page and have been using LINQ to handle talking to MS SQL server. I'm ok at basic SQL, however I'm a lot better with designing queries using LINQ. I know they are similar but I find it easer to design complex queries in LINQ. My question is this: Is there a way to design a query in LINQ and then get the SQL that it generated? I would like to embed the SQL in a stored procedure since multiple pages (outside my control) will need to do the same query.