LInq Combine duplicates
Hey everyone, trying to figure out how to combine my dates together so that it doesn't separate the data.
This is what I am currently working with
116 Replies
Basically need my Date section to not repeat but rather combine them into one date
GroupBy
Yeah I figured but I'm not sure what to group with here
Date
as you said
Where should I place my groupby? I was thinking after my where clause
But would it make more sense to do it earlier
do you know sql
Got a general understanding
think about the order
what do you do first
In the beginning
Is what I would think after I call orders
first statement
Yeah Okay I'll give it a shot
let's say i want to: group all my invoices by date that are more than 15.000 usd
what is the order
in sql
I would like to believe you would groupby
First
and then a where?
so, if you groupby first, what do you groupby? since you don't have anything yet
Ohhh
I should put it after my .where so I got access to the data
yeah so it goes something like this: get the data from somewhere, where some condition(s) apply, then group by some column, then order by some condition
Ahhhh okay
Issue is when I place my groupby in that location I get errors in my select
is that because of my orderby?
cause that's not where it goes
you want to groupby sales
not Store
Oh ok
Sorry Kinda new to this started last month
I believe i'm understanding what you're getting at though
where goes first
remember
select where groupby orderby
https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16#logical-processing-order-of-the-select-statement
Alright so this is the correct sequence
I need my group by after my where
So I need to add it before the orderby
but after the where
the GroupBy?
yes
When I put it directly after my where clause my orderby gets an error
what's the error?
Does not contain definition for OrderDate
that's because it's not where it goes, again you are returning 3 things:
1) City
2) Location
3) Sales: here you need to go deep, so you select again then you groupby then you orderby
does it make sense?
Trying to figure this out sorry if it's taking a while
no worries at all
So city and location is working as intended
For sales I first linked the two tables using a where clause
and I also added a month== 12 condition because I want it to only be dec
You are saying I should select and then group by and order by?
this works?
eah
Yeah
Yeah
Wait
Yeah that works
I just need to insert a group by into here somewhere but I just really suck at figuring this out lol
so let's go again
remove that orderby from there
now that you selected the sales what do you do
Okay it is now gone
I should group by
then order by
where
After the select
wait
which one
group by before select
orderby after
no
select starts everything
Alright sure
So I should group by prior to my order by
you are selecting a store
right?
at the beginning
Yeah
A store linking to orders
and then you are selecting sales
right?
.Select(bla)
Yeah
so where does the groupby and orderby go
are you grouping sales or stores?
orders
so sales
so..
I feel so dumb right now lemme give it a look
So within my select statement
I should be looking to insert a group and order by
you are not dumb
you are joining tables
sql sucks xd
if you don't like this approach just let me know and i'll tell you where i think it should go
I like learning though
I think I can learn from an example you give me
I actually absorbed a lot from that earlier list you sent
of the sql order
my example is, you select stores, then you select sales
where does the groupby go
and proceeding orderby
It should go after the select?
of what selection
date?
OrderDate
but obv my sales
yes
so you selected the sales
then you?
group by
I just don't know where in my code
then you?
but after I group by I add orderby
group first order second
show me where you select the sales
the .select
in the middle
so what comes after a select
Groupyby
okay
does it make sense?
Yeah but like Do I put it after everything i've sleected
like the gst product sales and stuff?
yes
at the bracket near the end?
Yeah I get an error when I add a group by there
what's the error?
OHH
Date I should use date
not orderdate
duuude
I've put it there so many times but that was the error I got
need to pay more attention xd
so I assumed it was in the wrong place
just to clarify, a select can be cut short, so in this case you are selecting 2 columns from store right?
then you want to join another table
so you start another select
and the ordering starts agian
again
i think it can be corrected a little bit though, not very good with EF
Alright but here is my issue
my orderby gives an error
what error?
that date time date, subtotal and gst do not contain definition for orderdate
they dont, you didn't select it
you selected Date
so o.Date
.whatever
is Date DateTime?
it's orderdate which is a datetime in the database
so
.OrderBy(o => o.Date.Day)
I had that and it didn't work
and it didn't work
but when I literally rewrote it
the exact same way
it worked
I think it was just a visual bug
it can happen
did u get the group and order now?
Oh wait when I click run
it doesn't work using date error doesn't show up til after
in the groupby or orderby?
and what's the error while compiling?
Cannot execute text selection: CS1061 'IGrouping<DateTime, <anonymous type: DateTime Date, decimal ProductSales, decimal GST>>' does not contain a definition for 'Date' and no accessible extension method 'Date' accepting a first argument of type 'IGrouping<DateTime, <anonymous type: DateTime Date, decimal ProductSales, decimal GST>>' could be found (press F4 to add an assembly reference or import a namespace)
can you show me Date
The database table?
model
Sales
sorry
can you do
Orders.Select(
).Where().GroupBy().OrderBy();
?
I'll give it a shot
My where starts to get an error that way
the store id
yeah
and orderdate.month
lost context
How would I go about fixing it so I link the tables and get the same condition
thinking
Never mind I think I found another solution
I've decided to re work some of it
oh nice
My issue now is that I can't access gst and subtotal
yeah cause they are in sales
can you try with a
SelectMany()
?Not sure if I I was taught to use that yet
maybe a join can ease things up:
like
yourquery().SelectMany(x => x.Sales.Sales, (x,y ) => // somethinghere);
Yeah I don't think I can do that
Doing some take home work
Why ?
oh
and i'm only allowed to use what I know or was taught
that might be something I learn at a later dae
uhm that's going to be though
Yeahhh
give me a sec
This question is crazy hahaha
going to have a look at your first question
So you are just actually trying to compact your dates as a
PARTITION BY
does
kindatry:
get your context for orders and stores
Yeah I gave that a shot no can do, I appreciate the help guys I've learnt a lot but I think I gotta wrack my brain at it
You can actually have an autoproperty inside the stores and doing then a
.Include()