EF possible Bug with return query - c#

I have a situation and I don´t understand. The case is very simple. I use the a generic repository to work my DB. http://efgenericrepository.codeplex.com/ All work very well, but now just 1 view I get a problem. I think the EF return a word data when I execute a query.
this is my SQL result in SQL Manager:
Select *
from Vw_HoursMOPJustificated
where IdUser = 20
and ActionDate = '2012-08-22' and Hour < 24
IdMopTime | IdJustification | IdJustificationType
44 30 8
44 40 11
44 43 13
45 31 8
45 41 12
46 32 8
And this is my result inside C# when I execute this simple code.
MyIGFEntities entity = new MyIGFEntities();
var table = new Repository<MyIGF.Models.Vw_HoursMOPJustificated>(new MyIGFEntities())
.Find(x => x.ActionDate == ActionDate && x.IdUser == IdUser && x.Hour < 24);
IdMopTime | IdJustification | IdJustificationType
44 | 30 | 8
44 | 30 | 8
44 | 30 | 8
45 | 31 | 8
45 | 31 | 8
46 | 32 | 8
Anybody can Help me?

You must correct your edmx (quite sure you have one).
On your Vw_HoursMOPJustificated entity and set Primary Key to true for IdMopTime, IdJustification and IdJustificationType (at least).
To check if everything is correct, try to get data from your edmx, and see if you have correct distinct data.
Sometimes (and mainly with views, which don't have a "real" primary key in db), the primary keys (or the properties which makes each row distinct) are badly retrieved, and you get this kind of confusing results.

Related

Uniformly distributing hash of given properties

I am trying to distribute a set of items across number of buckets. I am looking for following properties:
Bucket assignment needs to be deterministic. In different runs same
input should end up in the same bucket.
Distribution of data between buckets should be uniform.
This should work for fairly small number of inputs (e.g. if I want
to distribute 50 inputs across 25 buckets ideally each bucket will
have 2 items).
First try was to generate md5 from input data and form bucket from first bytes of md5. I am not too satisfied with uniformity. It works well when input is large but not so well for small input. E.g. distributing 100 items across 64 buckets:
List<string> l = new List<string>();
for (int i = 0; i < 100; i++)
{
l.Add(string.Format("data{0}.txt", i));
}
int[] buckets = new int[64];
var md5 = MD5.Create();
foreach (string str in l)
{
{
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(str));
uint bucket = BitConverter.ToUInt32(hash, 0) % 64;
buckets[bucket % 64]++;
}
}
Any suggestions what could I do to achieve higher uniformity? Thanks.
Leaving aside the efficiency of using MD5 for this purpose (see the discussion here and in the marked duplicate of that question), basically the answer is that what you have is what a uniform distribution really looks like.
That might seem counter-intuitive, but it's easily demonstrable either mathematically or by experiment.
As a kind of motivating example, consider the task of choosing exactly 64 numbers in the range 0-63. The odds that you will get one per bucket are very close to 0. There are 6464 possible sequences, of which 64! contain all 64 numbers. The odds of getting one of these sequence is about one in 3.1×1026. In fact, the odds of getting a sequence in which no element appears three times is less than one in a thousand (it's about .000658). So it's almost certain that a random uniform sample of 64 numbers in the range 0-63 will have some triplets, and it's pretty likely that there will be some quadruplet. If the sample is 100 numbers, those probabilities just get even bigger.
But the maths are not so easy to compute in general, so here I chose to illustrate by experiment :-), using random.org, which is a pretty reliable source of random numbers. I asked it for 100 numbers in the range 0-63, and counted them (using bash, so my "graph" is not as pretty as yours). Here are two runs:
First run:
Random numbers:
44 17 50 11 16 4 24 29 12 36
27 32 12 63 4 30 19 60 28 39
22 40 19 16 23 2 46 31 52 41
13 2 42 17 29 39 43 9 20 50
45 40 38 33 17 45 28 6 48 12
56 26 34 33 35 40 28 44 22 10
50 55 49 43 63 62 22 50 15 52
48 54 53 26 4 53 13 56 42 60
49 30 14 55 29 62 15 13 35 40
22 38 37 36 10 36 5 41 43 53
Counts:
X X X
X XX X X XX X X X X X
X X X XX XXX X X X XXX X XX XXXXXXXX XXX XX XX X XX
X XXX XXXXXXXXX XX XXX XXXXXXXXXXXXXXXXXXXXX XXX XXXXX X XX
----------------------------------------------------------------
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6
0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2
Second run:
Random numbers:
41 31 16 40 1 51 17 41 27 46
24 14 21 33 25 43 4 36 1 14
40 22 11 22 30 19 23 63 39 61
8 55 40 6 21 13 55 13 3 52
17 52 53 53 7 21 47 13 45 57
25 27 30 48 38 55 55 22 61 11
11 28 45 63 43 0 41 51 15 2
33 2 46 14 35 41 5 2 11 37
28 56 15 7 18 12 57 36 59 51
42 5 46 32 10 8 0 46 12 9
Counts:
X X X X
X X XX XX XX X X X
XXX X XX XXXXX X XX X XX X X X XX X XX XXX X X X X
XXXXXXXXXXXXXXXXXXXX XXXXX XX XXXX XXXXXXXXX XXXX XXX XXX X X X
----------------------------------------------------------------
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6
0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2
You could try this with your favourite random number generator, playing around with the size of the distribution. You'll get the same sort of shape.

Doing Calculations of a group

I have a Visual Studio Program where the user enters specific data of production for that day. Example:
Date | Part Number | Mold Num | Machine Num | Total Parts |Cycle
2/12/2016 | 1185-5B8 | 6580 | 12 | 220 | 56
2/12/2016 | 2249300 | 7797 | 36 | 600 | 13
2/12/2016 | 146865 | 5096789 | 56 | 500 | 15
2/16/2016 | 123456 | 7787 | 54 | 300 | 34
2/16/2016 | 123456 | 787 | 54 | 360 | 36
2/16/2016 | 123456 | 777 | 54 | 500 | 46
2/16/2016 | 123456 | 87 | 54 | 400 | 44
I'm trying to have people enter production data and then manipulate it in order to get the machine usage (MU) (Equation: Total Parts/(3600/Cycle). I want to get these things for each day and for a specific month when I call it. I looked at what others have done but they are just counting how many are in a specific date but I need to multiply, and divide other columns together in two different tables with the same dates in order to get what I need.
Example output:
2/12/2016: MU = 41.12%
2/16/2016: MU = 24.79%
February: MU = 32.96%
EDIT:
I would like to do something in the lines of this example but I do not know how to implement it to an already existing DataSet.
Grouping and Sum Datatable by two fields with different Where conditions
or this example:
Calculating percentage of a groups from datatable

Is it possible to pivot a SQL Server table with multiple data colums

I have a table like this to be exported to Excel
name date value1 value2 value 3
A 09/09/2015 5 10 2
B 09/09/2015 6 6 22
C 09/09/2015 4 3 11
A 10/09/2015 15 1 2
B 10/09/2015 6 16 27
C 10/09/2015 4 31 11
A 11/09/2015 15 1 2
B 11/09/2015 6 16 27
C 11/09/2015 4 31 11
can we pivot this to something like this (using SQL or C# datatable)
09/09/2015 | 10/09/2015 | 11/09/2015
value1 value2 value3 | value1 value2 value3 | value1 value2 value3
A 5 10 12 | 15 1 2 | 15 1 2
B 6 6 22 | 6 16 27 | 6 16 27
C 4 3 11 | 4 31 11 | 4 31 11
since your exporting this to Excel why not sumarize the table in Excel into a Pivottable and from there put the name column into the row box, date column into column box, the values into the value box.
After that you can insert some nice diagrams and then you have a nicer view of the data if you ask me.

Changing the Rows state in DataTable if it meets the following condition

i have two tables: DataTable dt_o; and DataTable dt_m;
Contents of dt_m:
ID | Name | Age
--------------------------------------
08 | Farel | 18
07 | Ema | 18
10 | Sophie | 19
11 | Cyril | 12
Contents of dt_o:
ID | Name | Age
--------------------------------------
08 | Farel | 19
07 | Ema | 18
10 | Sophie | 19
the row from dt_m with the ID of 08 should be set to row.SetModified() since once of it's columns value where different in the other Table[dt_o]
and the row from dt_m with the ID of 11 should be set to row.SetAdded() since it doesn't exist in the other table...
and the rest of the row, there row states would be set to unchange state..
any code suggestion?
I would search the tables for the specified requirements you're seeking using the answer of this post: how to search the dataset for specific data
Then just set the state of the rows returned based on what you wanna do. For example:
dataSet.AcceptChanges(); //If you'd like to start with everything as "unchanged"
foreach(DataRow row in rowsReturnedFromQuery1)
row.SetAdded();
foreach(DataRow row2 in rowsReturnedFromQuery2)
row2.SetModified();
Hope this helps. I'm sure you can do this using Linq as well.

SQL string concatenation based on field value

I am wondering if there is a straightforward way of concatenating a string based on the value of a field directly in SQL. I know it would be preferable to do in an application, but in this instance, I am only able to use SQL. For example, the following table:
Labels | Qty | LabelQty | OutputString
-------+-----+----------+--------------
1 | 30 | 30 | NULL
2 | 60 | 30 | NULL
2 | 120 | 60 | NULL
I would like to end up with the OutputString like so:
Labels | Qty | LabelQty | OutputString
-------+-----+----------+--------------
1 | 30 | 30 | 30|
2 | 60 | 30 | 30|30|
2 | 120 | 60 | 60|60|
I know this is very easy to do in C# or VB, but I am having a hard time thinking about how to accomplish this in straight SQL. Would I need to use a cursor and do each row one at a time? Unfortunately, I can't just use the string multiplier like in Ruby, such as:
SELECT (CONVERT(VARCHAR(10), LabelQty) + '|') * Labels
Any pointers are much appreciated.
You can use REPLICATE() function:
select replicate(cast(LabelQty as varchar(100)) + '|', Labels)
from mytable_1
You can try
SELECT REPLICATE(CONVERT(VARCHAR(10), LabelQty ) + '|',Labels )
While you haven't described how to produce output string, but I think that this can be done with case expression:
http://technet.microsoft.com/en-us/library/ms181765.aspx
And sample from that article:
SELECT ProductNumber, Name, "Price Range" =
CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber ;

Categories

Resources