I have around 1 million records, witch are for answering questions during 24 days (one per day).
What I'm trying to accomplish is gather all correct answers throughout the days and make then a lottery selection, like, if a subscriber answered all 24 answers correctly he/she has an higher probability of be selected than a subscriber that only answered correctly one answer.
I'm using Linq-to-Sql as I fell more comfortable of using it, rather than pure T-SQL, even though this would be to be porter to T-SQL, but LinqPad helps me on that, so here's what I've got so far:
// all correct answers id's
var correct = from a in JK_ChallengeAnswers
where a.correct
select a.challenge_answer_id;
// all correct answers
var query = from cr in JK_ChallengeResponses
where correct.Contains(cr.challenge_answer_id)
select cr;
query.Count().Dump(); // 978144
var random = new Random();
int pos = random.Next(query.Count()); // random select
query.Skip(pos).Take(1).Dump(); // get one...
This is straight forward ... and it's linear, there is no "ticket" approach, here a person that answered 1 question as the exact percentage of winning of the one that answered all 24...
How would I go and try the lottery approach, what should be my next step?
No need to show me code, I can get that, only the steps, cause I'm not getting them somehow.
Create a table of lottery tickets.
For each subscriber, put their entry in the table once for each question they got correct.
Choose a record from that table at random.
Create a view with all the people's names that have answered correctly. Then you can use a random number generator to select the person whos id matches the random number. If you need more help ask me.
EDIT
To me, the lottery approach is already working for you because each person can guess an answer every day for 24 days. That means they have 24 answers in your table with the foreign key that points to the correct answer. By doing this, I have a better chance of getting the right answer if I submitted 24 entries into the database. This would make my approach work.
Let me know if you had a different idea for how this would work.
Related
I know similar questions have been asked, but I have a rather different scenario here.
I have a SQL Server database which will store TicketNumber and other details. This TicketNumber is generated randomly from a C# program, which is passed to the database and stored there. The TicketNumber must be unique, and can be from 000000000-999999999.
Currently, what I do is: I will do a select statement to query all existing TicketNumber from the database:
Select TicketNumber from SomeTable
After that, I will load all the TicketNumber into a List:
List<int> temp = new List<int>();
//foreach loop to add all numbers to the List
Random random = new Random();
int randomNumber = random.Next(0, 1000000000);
if !(temp.Contain(randomNumber))
//Add this new number to the database
There is no problem with the code above, however, when the dataset get larger, the performance is deteriorating. (I have close to hundred thousand of records now). I'm wondering if there is any more effective way of handling this?
I can do this from either the C# application or the SQL Server side.
This answer assumes you can't change the requirements. If you can use a hi/lo scheme to generate unique IDs which aren't random, that would be better.
I assume you've already set this as a primary key in the database. Given that you've already got the information in the database, there's little sense (IMO) in fetching it to the client as well. That goes double if you've got multiple clients (which seems likely - if not now then in the future).
Instead, just try to insert a record with a random ID. If it works, great! If not, generate a new random number and try again.
After 1000 days, you'll have a million records, so roughly one in a thousand inserts will fail. That's only one a day - unless you've got some hard limit on the insertion time, that seems pretty reasonable to me.
EDIT: I've just thought of another solution, which would take a bunch of storage, but might be quite reasonable otherwise... create a table with two columns:
NaturalID ObfuscatedID
Prepopulate that with a billion rows, which you generate by basically shuffling all the possible ticket IDs. It may take quite a while, but it's a one-off cost.
Now, you can use an auto-incrementing ID for your ticket table, and then either copy the corresponding obfuscated ID into the table as you populate it, or join into it when you need the ticket ID.
You can create a separate table with only one column . Lets just name it UniqueID for now. Populate that column with UniqueID = 000000000-999999999. Everytime you want to generate a random number, do something like
SELECT TOP 1 UniqueID From (Table) WHERE UniqueID NOT IN (SELECT ID FROM (YOUR TABLE))
Code has not been tested but just to show the idea
I have been attempting to research the best way to do the following:
Save a user-defined text based expression to a database which outlines content to be displayed on a page.
example: "The employment status of this person is a [employment] employee, [hours] hrs a week."
Where [employment], [hours] correspond to a key/value in a database
example:
TABLE
ID KEY VALUE
1 employment casual
1 hours 10
2 employment parttime
2 hours 25
At runtime a page where this user-defined expression would be used for display has access to a persons id and would need to collect the relevant details from the database, parse and replace the [employment], [hours] placeholders with the retrieved data.
example: "The employment status of this person is a casual employee, 10 hrs a week.
The solution I am looking for needs to be dynamic enough to handle a user entering any static text in and around any number of placeholders.
EDIT to clarify my meaning:
There will be a settings area where the user can enter the expression they want
displayed on the screen, which could be any text containing any 'key' values from
the database. Different users will see their own defined expression displayed.
another example: "The employee works [hours] hrs, [employment]"
would display as: "The employee works 10 hrs, casual"
I'm either not doing very well with my searching or just not understanding the results very well. I have found a lot of information regarding CustomExpressionBuilder, Lambda expressions, Expression trees, etc. and I am getting confused whether any of these are the right track for what I need to do. Perhaps I'm just not there yet on my understanding being new to this!
If I can get this working I would also love to be able to add some simple ternary type functionality and/or mathematical functionality. It seems like there is library called NCalc that would make this quite simple, but I could not see an example of the text replacement requirement that I initially need, or immediately see a way to tweak it to work.
Thanks for any advice, and I am more than happy to research further but need a little push in the right direction.
From your question, it's a bit vague as to how far you've gotten with this problem, ie if you are already able to retrieve all of the values from the database or not.
If you are simply trying to build a string expression as you mentioned above, once you have the values above, I would probably use either String.Format or perhaps a string builder.
For example, if we presume that you have the values out of the database and they are contained within properties on a Person class (just as an example), you could write something to the effect of:
var message = String.Format("The employment status of this person is a {0} employee, {1} hrs a week.", person.Employment, person.Hours);
Like I said, it's a bit vague from your question exactly where your issue is, but if you're just looking to build that one particular expression once you have the values, the above would work. You can replace the person.Employment and person.Hours arguments with the appropriate values once you have those out of the database.
Which system would be the best to go ahead with for voting function and why? ;
Putting the votes separately inside a database table and calculating the average when it is needed
Having only one row for one product. When a new vote comes, getting that vote form database, calculate it again and update the record. for example, we have a vote record for product 1. Its vote is totally 326 inside database and 45 people have voted. then we receive a new vote which is 4 and for product 1. We take the record from database and then do the following function;
(326 + 4) / (45 + 1)
then save it to database again with total value of 330 and 46 as voted people.
I always go ahead with the first option but I wanna know what others are doing.
Well, it always depends on what you want to with the additional information about the votes.
If you want to know for example trends in voting (is the average going up or down) or you want to know it the votes are relatively old / new or what user has casted the vote (the 1-vote-per-person-check) etc. etc. etc. you want to store each vote in a separate table.
If all you care about is the end result the second option is way more efficient.
If you want to suppress that people can vote twice you will need the 1-row-per-vote approach anyway, so I recommend the first option.
The second option might be better performance-wise - but should that ever be a problem, it's easy to use a caching-mechanism.
There are lots of factors to consider. Are you needing to defend against someone attacking your application server and modifying the vote count? Are you concerned with people's votes being discoverable? Are you concerned about locking-related performance issues? All of those will affect the decision, and the answers to those vary wildly depending on the specific scenario.
I am working on program that calculate the GPAs for university students in C#.
I have made a gridview that contains columns for each subject. Its mark as letter and
number. After the user "student" insert his/her subject I want to let the user click a
button that will get the mark and the number of the credit hours of each subject. Calculate the GPA for the student and put the result in a textbox.
I am having trouble getting started. Any advice for a good place to start?
To answer just the question in your title:
To get value from the current row, first capture the row:
DataGridViewRow r = MyDataGridView.CurrentRow;
To get the value from cell 0:
string v = (string)r.Cells[0].Value;
What I see from your question you are having difficulty how to put the requirement in one place.
I'm assuming you already have the user requirements (it looks very simple) and I give basic steps you go through, it is not accurate and it doesn't fit for all type of projects, but your case is very simple and I think it is enough to do the work.
first you will need to create a flow, the steps the user will go through to accomplish the process.
then you will need the basic interface to let you have at least to know what controls you will need.
after that you will make the data structure (mostly the database schema) to know how the data will be stored and related to each other.
then you will create the business logic to implement your requirements.(the coding part)
and Last you will glue all the things and attach the code that wrote to the events in the interface based on the flow you made it at the first step.
I hope this helps.
I'm not sure about what you are trying to do, but you can take a look here:
http://www.c-sharpcorner.com/Articles/ArticleListing.aspx?SectionID=1&SubSectionID=191
I've solved lots of my WPF dumb design problems with these articles :P
I am trying to have a ratings strategy in the hotels search website.Ratings is based on number of clicks by the users who view different hotels.I am trying to assign number of points to each click.
Suppose i have a POINTS column in the hotels table which increases by the number of clicks on each hotel, the problem i face is suppose i have a hotel1 which was introduced in week1 then it gains considerable amount of points by week2 but if i introduce a new hotel2 at week2 although this new hotel is competetively increasing the points it cant reach easily the hotel1 becoz of there difference in weeks which they were introduced.
I thought a way to solve the above problem by introducing a DAYS column then i can divide the POINTS of each HOTEL by number of days so that i can have clear ratings to each hotel.
I need your help about how i get the number of each passing day in the DAYS column after new hotel is added in the table of database.
It would probably be better to have a CreateDate column, and then in client side code do something along the lines of:
int days = Date.Now.Subtract(hotel.CreateDate).Days;
This will cause less updates to your database too, as the date only needs to be set on create.
I'm not going to go into the statistical theory of what you're doing, but let me state for the record that I think your stats are going to be misleading.
Be that as it may, just to achieve what you say you want to achieve, I would do what #Andy just said you should do. (Beat me to it!)
As I understand it, you are asking people to "vote" on the hotels, but the only possible votes are "yes" and "no vote".
Whether this is statistically valid will depend on when people are asked to vote. If every time someone visits a hotel, you ask him whether he was satisfied with his stay, and people consistently do this, I suppose it would be generally valid. But if the scoring system is such that users do not perceive a need to re-vote on a hotel that they have already voted on, then hotels that are on the list longer will see their ratings tend to sink. If it reached a point where every user who had visited a hotel has voted (or not), and no one saw a need to vote again, then that hotels score would gradually sink.
Also, this system would be biased in favor of big hotels. If hotel A has 500 rooms and hotel B has 10 rooms, it would b e very tough for hotel B to ever get as many votes as hotel A.
I think you'd be better to ask people to rate the hotel on some scale -- 1 to 5 stars or whatever -- and then present the average score. Probably along with the number of ratings, as people can probably figure out that if there's only one rating and it's the highest possible, that might be the owner rating his own hotel.
An alternative to calculating the days in code would be to use a computed column in the database (assuming by the sql tag you meant sql server). As the other posters have said, add a CreateDate column for the hotel and then add a computed column to return the date diff.