« first day (3622 days earlier)      last day (1309 days later) » 

9:07 PM
List<BlogContents> blogs = GetBlogsAll();

                    List<Orders> orders = GetOrdersAll(sessionId);

                    List<ShoppingCart> carts = GetCartsAll(sessionId);

                    List<BuyDTO> result = null;

                    result = (from articolo in blogs
                              join order in orders
                              on articolo.Id equals order.Id_Articolo
                              from item in blogs
                              join cart in carts
I expect a result set of three records and this query returns six
can anyone tell me why???
somehow the result set is doubled if I should have two elements I have four and so forth
 
mr5
Did you override the .Equals method of BuyDTO?
 
@mr5 what do you mean by that?
 
Just a wild guess but did you try to groupby
 
@sjehutch group by what?
I mean it's a join on three tables which I've tried it out on sql management studio and it works as expected over there
for example I expect two elements in the result set and I have two
without using the group by
that's why I asked you that question
 
mr5
@Riccardo just override the .Equals and .GetHashCode of BuyDTO and execute the LINQ again.
 
9:17 PM
@mr5 I don't know how to do it could you give me a sample code please?
 
mr5
ok
shift the focus in BuyDTO class in your IDE
 
ok
 
mr5
type in the class body: "Equ" + ctrl + space
the intellisense should trigger the auto complete and press enter
 
I have to type "Equ" and press those keys?
 
mr5
yeah. 'e', 'q', then 'u'
let me know if it works
of much better: type 'equ' + tab (press twice as fast as you could)
 
9:21 PM
@mr5 i've typed equ
 
mr5
that should automatically create the method for you.
 
wait a sec
@mr5 it created thihs method
then what?
 
mr5
implement the equality check
 
how can I implement it?
buy dto contains properties that are populated after the three joins
 
mr5
how do you distinct a BuyDTO from another?
 
9:23 PM
and the equal method requires an input parameter that I don't know how to provide
 
mr5
no. you don't need to provide yourself.
the LINQ will use it later.
which property of BuyDTO is distinct from another BuyDTO?
 
// override object.Equals
        public override bool Equals(object obj)
        {
            //
            // See the full list of guidelines at
            //   go.microsoft.com/fwlink/?LinkID=85237
            // and also the guidance for operator== at
            //   go.microsoft.com/fwlink/?LinkId=85238
            //

            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            // TODO: write your implementation of Equals() here
@mr5 this is what it did but it says i have to implement those methods
 
mr5
focus on the TODO comment.
yeah. answer the question I have asked you so we can come up with the solution.
 
should I just comment those throw new exceptions and use the returns
 
mr5
delete the throw statements.
 
9:26 PM
let me comment them out
whatever I've got the default implementation
what about now?
 
mr5
4 mins ago, by mr5
how do you distinct a BuyDTO from another?
 
should i call this method inside the distinct?
like Distinc(Equals)???
 
mr5
no
 
then what?
 
mr5
4 mins ago, by mr5
which property of BuyDTO is distinct from another BuyDTO?
 
9:28 PM
its article id
 
mr5
finally!
 
and its session id
 
mr5
what.
 
well article id is foreign key in two tables
and I use it as join condition on both of the joins
while session id I use it as where condition
so what?
 
mr5
return obj is BuyDTO dto && dto.ArticleId == ArticleId && dto.SessionId == SessionId;
now put that in the return statement
 
9:32 PM
@mr5 isn't that more a where clause?
I mean i expect to return a list of objects of type buydto
which i may call result
so return resut
 
mr5
internally, .Distinct() uses the .Equals method of the type. If it's an anonymous type, it will generate its own equality comparer.
this is just my assumption but is highly likely the case. So you need to distinct one element to another.
 
@mr5 after implementing the ovverride what is next?
 
mr5
and in the GetHashCode(), for the mean time, just return ArticleId.GetHashCode()
 
I mean the join on three table returns a list of buydto called result
 
mr5
re execute your query.
 
9:35 PM
wait a second
in the equals method I need an input parameter
cause that method is in the dto class
while the query is in another class
 
mr5
dude, don't focus on it. it will be used internally by .Distinct.
don't call it explicitly.
 
ok then how can I use articleid if it is a property?
 
mr5
leave alone the rest of your code untouched and do as I say.
did you re execute the query?
 
to do as you say I need to understand you
I didn't yet cause I'm trying to comprehend you
my fault but I miss something
 
mr5
@Riccardo can you elaborate on this?
 
9:38 PM
I just implemented the override and left the query as I showed you earlier
so as for now the query is not modified
what do I need to do on the query statement
 
mr5
what are the values of SessionIds, ArticleIds on the result?
 
session id is the only parameter which I evaluate against the query
article id is not a parameter
@mr5
 
mr5
the way .Distinct works is it enumerates the collection you are after, checking one element from another. It will store all distinct elements in a collection by using the .Equals method of the type being enumerated.
I can't tell you further what is wrong unless you show me the values of the result of your query.
the other where clauses in your query is disregarded since you have already formed the list before the .Distinct is called.
 
result = (from articolo in blogs
                              join order in orders
                              on articolo.Id equals order.Id_Articolo
                              from item in blogs
                              join cart in carts
                              on item.Id equals cart.Id_Articolo
                              where order.SessionId == sessionId && cart.SessionId == sessionId
                              select new BuyDTO
                              {
                                  Id = order.Id,
check again the code I've got just one where
 
mr5
I am asking about the values, not the code.
 
9:46 PM
ok the values are doubled
 
mr5
you said it didn't change after you override the equality method.
 
I didn't execute the code yet
 
mr5
then look for the properties you said that are supposed to be distinct.
 
i haven't tried it
I've just implemented the equality method in the dto class
stop
 
HAMMERTIME!
 
9:47 PM
the only distinct is by session id
 
HAMMERTIME!
 
which i store in two tables
so again what do I have to do after just implementing the equality method?
try to execute the query?
or do I need anything else?
@mr5
 
mr5
hold on a sec
fiddle with this. toggle the .GetHashCode and .Equals method of the BuyDTO so you can see how it actually works.
proper implementation of .GetHashCode: dotnetfiddle.net/cSzGCS
 
@mr5 that's what i did
// override object.Equals

public override bool Equals(object obj)
{
return obj is BuyDTO dto && SessionId == dto.SessionId && Id_Articolo == dto.Id_Articolo;
}

public override int GetHashCode()
{
return SessionId.GetHashCode();
}
what about now? just execute the query?
 
mr5
do this:
3 mins ago, by mr5
proper implementation of .GetHashCode: https://dotnetfiddle.net/cSzGCS
my bad, it needs a proper implementation of .GetHashCode
 
10:03 PM
corrected as you showed me
now?
public override int GetHashCode()
        {
            int hashCode = 2118541809;
            hashCode = hashCode * -1521134295 + SessionId.GetHashCode();
            hashCode = hashCode * -1521134295 + Id_Articolo.GetHashCode();
            return hashCode;
        }
execute the query?
 
mr5
I forgot you can also auto generate this in VS by: right click on target class + Quick Actions and Refactoring + Generate Equals and GetHashCode + Select the properties that are distinct from another.
yes.
 
using distinc without parameters right?
 
mr5
yes
 
just execute the query as is
ok let you know asap
 
mr5
also
3
Q: How does Distinct() work?

ojekLets say i have this: class Foo { public Guid id; public string description; } var list = new List<Foo>(); list.Add(new Foo() { id = Guid.Empty, description = "empty" }); list.Add(new Foo() { id = Guid.Empty, description = "empty" }); list.Add(new Foo() { id = Guid.NewGuid(), descriptio...

 
10:16 PM
@mr5 I've executed the query the result set expected is correct one only thing
property type category of BuyDTO is always the same
it's the only property which is always set to the firs element of the result set
 
mr5
right, so it leads you to the same question again:
54 mins ago, by mr5
how do you distinct a BuyDTO from another?
54 mins ago, by mr5
which property of BuyDTO is distinct from another BuyDTO?
 
@mr5 so I need to add category and description properties to the method
the same way you did for those two
am I right?
 
mr5
is the Id property not enough to distinguish it?
Id = order.Id,
this is a wrong assumption.
you should rename your Id to OrderId
 
@mr5 no
in the method you implented for me you entered two parameters
article id and session id
maybe i can try to add category too
why shouldn't I?
@mr5 this is what i've tried
public override bool Equals(object obj)
        {
            return obj is BuyDTO dto && SessionId == dto.SessionId && Id_Articolo == dto.Id_Articolo && Category == dto.Category;
        }

        public override int GetHashCode()
        {
            int hashCode = 2118541809;
            hashCode = hashCode * -1521134295 + SessionId.GetHashCode();
            hashCode = hashCode * -1521134295 + Id_Articolo.GetHashCode();
            hashCode = hashCode * -1521134295 + Category.GetHashCode();
 
mr5
it seems the description property is a string type which is an inefficient candidate to be one of the identifiers.
idunno.
 
10:52 PM
@mr5 there are some properties which stay set to the first element of the result set
these properties are quantity description and category
which belongs respectively to the same entity
all the other properties change accordingly
why?
 

« first day (3622 days earlier)      last day (1309 days later) »