3

I use the following code :

List<vw_GetIMeasurements> Imeasurements = context.vw_GetIMeasurements.Where(f => f.MEMBERID == userID).Distinct().ToList();

This returns a list with 12 Values like it should but these values are always the same(even though they aren't in the DB.(Even if they were distinct should solve this)

Visual of the issue

DB :

Database Image

The raw value that is returned is also wrong. What would be the cause of this issue and how can I resolve it?

5
  • 1
    Does vw_GetIMeasurements implement Equals and GetHashCode? Commented Jul 17, 2012 at 12:18
  • Can you provide some testdata please? Commented Jul 17, 2012 at 12:19
  • Yes, it does implement those. Commented Jul 17, 2012 at 12:20
  • 1
    Distinct uses the Default Equality Comparer (at least I believe that) Commented Jul 17, 2012 at 12:25
  • Your backing view has something wrong with it Commented Jul 17, 2012 at 12:43

4 Answers 4

8

I have had this problem before - it turned out that the view did not have unique entity keys set - make sure your primary keys for your view that is auto created by entity framework is indeed unique...

Sign up to request clarification or add additional context in comments.

3 Comments

I can't change the Database, is there any other way to make this happen?
You dont to need change the database - its the model (.edmx file) in your designer, make sure the entity keys are unique enough, the EF will try to create a unique key but it doesnt always get it right for views.
The issue is that all of these values aren't unique , so they always appear multipel times. I think I can solve the issue I have by Selecting the 2 colums that I need and not the entire row.
2

I had a similar issue - the database view looked fine in SQL server but when debugging, duplicate rows were appearing where rows shared a particular attribute value.

I found that one of the important unique keys was from a outer join table - therefore had the potential to be null - and the model was not assigning as unique.

I was able to fix this problem by setting this field to ISNULL(field, '') - ensuring that a value is always returned - and upon updating, the model correctly interprets the field as unique and as part of a compound key and the rows returned correctly.

I know that one might argue the view design is at fault (i.e. I should not be using an outer join), but in this instance, there was a specific need for the outer join. The fix works because the compound key is still unique even if "" is returned multiple times.

Comments

0

Maybe try a more simple syntax like this

var measure = from f in context.vw_GetIMeasurements
              where  f.MEMBERID == userID
              select f;

This kind of thing is working for me..

3 Comments

Well I'm trying to get back a list of multiple values. How would this be done?
This kind of linq request return an IENUMERABLE<T>. You can Apply a .Tolist() after that to get a List<T>
Sadly enough this gives the sames values as in the question. EVen the raw value is wrong.
0

The issue is infact what Richard described. However I was unable to choose a correct "Unique Key" so this caused more issue.

I solved my problem by creating a custom made class and creating this in SQL. I also limited my Select to 2 of the 3 Columns.

List<Measurement> result = (from f in context.vw_GetIMeasurements where f.MEMBERID == userID select new Measurement { Category = f.atcmCATEGORYCODEID, Size = f.imMeasurementNumber }).ToList();

Measurement is in the case the self made Class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.