C# - Linq2sql Count Grouping Vote Entity Query - Can't Get Properties Once Grouped?
Oct 11, 2010
I'm building a music voting application, similar to stack overflow almost.I have 3 tables, Charts, ChartItems, Votes.I'm trying to bring back a list of chartitems, which link to a single chart, with the number of votes for each chart item counted.This is what I am trying currently
var firstList = from chartItem in db.ChartItems
join vote in db.Votes on chartItem.ixChartId equals vote.ixChartId into j1
where chartItem.ixChartId == id[code]...
The problem is once I have performed the grouping, I can't get any of the other properties I need from the join to populate the model. For example each ChartItem should have a title, id etc...I have created a ViewModel to hold all the properties that I need, called ChartItemWithVotes(includes all entity values + int totalVotes)In the end I am looking for this
Chart Name
Votes Name
20 - ChartItemname
15 - ChartItemname
12 - ChartITemName
View 3 Replies
Similar Messages:
Mar 26, 2011
I'm currently learning entity framework (from the Julia Lerman book "Programming Entity Framework") and got really confused at the grouping part.
var contacts = from c in context.Contacts
group c by c.Title into myGroup
select myGroup;
This is supposed to return a Grouping<K,T> type, but the book didn't give an example on how exactly do I access this "contacts" query and I can't figure it out. When I did foreach(var contact in contacts), my "contact' variable has no knowledge of any of the properties of the Contact type (contact.FirstName etc).
View 1 Replies
Nov 25, 2010
I am having a meltdown over something that seems so simple and yet isn't working. Here's my scenario.
I have an object structure of tEvents, which contains properties of an event like a run of concerts. tEvents in turn contains an entity set of tEventOptions, which include properties like EventDate, Cancelled etc. I would like to query a list of tEvents using properties of the tEventOptions, for example filtering by date.
My pageis using a calendar object to show event dates. So I am trying to find if any tEvents in the List coming back from the database match the date of the day element being rendered in my Calendar control like so (in this snip 'data' is List and results from a db query):
[code]....
Except r, my query result var, is always true no matter what data it receives. I have tried the sub query using other properties of tEventOption and get the same 'true' result each time. I know that the data does not reflect this result so I clearly have a problem with the structure of my query, but for the life of me I cannot find a way to resolve it.
View 1 Replies
Oct 22, 2010
I'm wondering about if there is a difference (in performance) when I use:
User usr = (from u in Adapter.User
where u.Login == login
select u).FirstOrDefault();
or
User usr = Adapter.getUserByLogin(login); //stored procedure
View 1 Replies
Jan 21, 2010
I've got a query such as
Dim MediaQuery =
From m
In dB.DOWNLOADS _Where m.ID = id _Select
which returns a record from the database. One of the fields in the record is a FK to another table. I need to read this value to be able to set a dropdown based on the ID but I can't work out how to access it. For a standard record I can just do the following txtTitle.Text = MediaQuery.FirstOrDefault().TITLE
However with the foreign key it doesn't work like that. I've tried drpGroup.SelectedIndex = MediaQuery.FirstOrDefault().DOWNLOAD_GROUPS.ID where DOWNLOAD_GROUPS is the FK field but it returns Object reference not set to an instance of an object. If you're simply wanting to read some values from a single db record in the entitiy framework and one is a foreign key how should I go about getting the value?
View 2 Replies
Jan 29, 2010
Is there a way to get ADO.NET Entity to generate 1:1 navigation properties? If I do foreign keys I always get 1:N navigation properties.
View 2 Replies
Oct 1, 2010
I've been learning MVC 2 and I have pretty much everything understood except for the model part of things, I understand what the model is but actually implementing it has me confused.
Here's my situation, I have my DB which has 3 tables;
Ideas - table of ideas
Tags - table of tags
IdeaTag - link table connecting the above 2 tables via FKs
So when using the Entity Framework (.edmx) designer in VS2010 I get 2 classes created in the Designer, which obviously map to my DB tables and Navigation Properties in Idea for Tags & Idea for Tag.
So this is all fine until I actually try to add tags to an idea, what's the best practise for dealing with Navigation Properties? I wanted to add a Textbox which will then map to the Tags property in the Idea class but I'm unsure how I'd go about this.
Most of the MVC tutorials which discuss EF or Linq to SQL are quite basic.
View 1 Replies
Feb 12, 2010
Tables: Article, Author, Comment (1 article and 1 author can have * comments)In the database is 1 article, 1 author and 1 comment.
The problem is, that code
myBD my_bd = new myBD();
var articles = by_bd.Article;
works OK, I can see that an Author and an Article has 1 comment. But that code
var comm = (from u in my_bd.Comment
where ......
select u);
returns the comment but it has NULL values in property Article and Author. Why ?
View 2 Replies
Jan 22, 2011
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.
View 2 Replies
Dec 27, 2010
I have a table students (studId, student) and 1000 records
1. I want to see in how many distinct name, thier frequequency
2. show only the records with frequency greater than 3
View 2 Replies
Apr 25, 2010
I'm face of a little problem when using GridView, an EntityDatasource and entity custom properties using a partial class. I'll describe it and I hope that someone has already find the solution because I try to figured out since two days.
View 4 Replies
Aug 25, 2010
I have a class that being used to connect with the DB. Now I want to count how many times each web request executes the queries, but I've no idea where to store the counted value. I mean, Session wont, ViewState wont work as site also have webservices. What else I can use?
View 12 Replies
Apr 18, 2010
How do I cound the records found by an SQL query? I have searched google and have only found ways to do so in vb.net. I need to know how to accomplish this in C#.
Below is what I have made so far...
Default2.aspx file...
[Code]....
Default2.aspx.cs File...
[Code]....
View 5 Replies
Feb 9, 2011
I have many record in my sql table and i want to fetch last row of table.I have used max function but it is very slow
i m using this query-
select pm_name from datos where date_time = (select Max(date_time) from datos) but it takes more time i want to use select count(date_time) from datos this query provide me row number then how can fetch row of this number.
View 11 Replies
Apr 18, 2013
The query I used here is
SELECT top '"+lblCount.text+"' Company_Id, CompanyName, All_Countries,All_Industries,Employees,Revenue, Status FROM SN001CompanyTable where Client_Id=100 ORDER BY [Company_Id] DESC
but its not showing proper results..
In this lblCount contains the no of row to be displayed..
View 1 Replies
Mar 28, 2011
Dim p = From i In context.persons
Order By i.fileNum
Select i.ID, i.fileNum, i.firstName, i.lastName, i.state, i.prPenPals.FirstOrDefault.startProfile, i.prPenPals.FirstOrDefault.endProfile
where for every person, prPenPal can have exactly one or zero records? I can do the above, but when I try to use p as the datasource of a ListView I get The cast to value type 'DateTime' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
I tried using
Select i.inmateID, i.fileNum, i.firstName, i.lastName, i.state, i.prLegals.FirstOrDefault.startProfile.ToString,
i.prLegals.FirstOrDefault.endProfile.ToString
but that didn't work. I got a blue squiggly line "Range variable name cannot match the name of a member of the 'Object' class" error I want the date if the record exists, and Null or Nothing if it dosen't.
View 1 Replies
Jul 12, 2010
Hi guys I need to rewrite the sql query below using Linq to entity. Unfortunately I don't have a lot of experience of using Linq.
With TempTableName AS
(SELECT [ColumnName],
[ColumnName2],
[ColumnName3],
[ColumnName4],
ROW_NUMBER() OVER (order by ColumnName desc) as RowNumber from TableName )
SELECT
[ColumnName],
[ColumnName2],
[ColumnName3],
[ColumnName4]
FROM TempTableName WHERE ROWNUMBER
Between 10 and 100
View 1 Replies
Aug 21, 2010
I doing the feedback system. I need to gather the user vote and perform calculation, and store into db.
View 1 Replies
Jan 26, 2014
How can we prevent non ahuthenticated users from like and dislike multiple times?one way : save Ip address in a cookiebut theres's two problem1. I heard Ip address isn't unique and changes frequently2. cookie can be deleted by userI'd .
View 1 Replies
Aug 30, 2010
I have added a partial class to one generated by the Entity Framework. I wanted to add a calculated field that I could bind to my GridView. However, when I try to access the new property, I get an error message that this is a limitation to Entity Framework.Are there any work arounds to accomplish this?
View 1 Replies
Oct 27, 2010
I have the following related tables:
Users
Id
Username
Email
Password
UserStatuses
Id
Name
Users_UserStatus
Id
User (FK - Users: Id)
Status (FK - UserStatuses: Id)
StatusDate
As you can see, UserStatuses is a reference table with various statuses. Users_UserStatus is a join table between Users and UserStatuses. Using Entity Framework, how can I do a conditional to check if the latest StatusDate is "Pending"? As of now, I have the following:
[Code]....
View 1 Replies
Mar 14, 2011
I have a simple Entity query:
[code]....
I then check whether one of the result values is more recent than six months ago using an if..then like so:
If rhcexists.First.SignatureDate > Date.Today.AddMonths(-6) Then
End If
However, if there are no results returned this will return an error. Is there a way for me to tell it to act as if the date is older than six months if there is no value at all? e.g., could I in pseudo do something like:
If Exists(rhcexists.First.SignatureDate, Date.Today.AddMonths(-8)) > Date.Today.AddMonths(-6) Then
End If
View 2 Replies
Mar 10, 2011
I want to create a site which is based on users submitting a suggestion and then other users voting the suggestions up or down. Very much like the Dell [URL] site. I was wondering if there might be an open source or other existing software I could leverage for this purpose
View 1 Replies
Jan 26, 2010
Need to figure out the correct way to do a simple SUM function in Linq, but also manipulate one of the feilds coming back by doing a Substring on it, here is the original SQL query:
[Code]....
View 1 Replies
Dec 4, 2010
I have this query for getting data through Entity Framework, which works today. Thing is, i want to create the query dynamically, i.e build it up in blocks, but i can't figure out how exactly to do it. I believe if i can somehow save one of the values that i retrieve in the beginning i can do it, but now i retrieve it twice (probably a bit slow as well? Unless the compiler fixes it before quering). Makes sense? Here is the LINQ:
[code]....
I.e based on if various variables have values.
View 1 Replies