How to return unique column with the max version of another column in Linq to SQL? - linq

I need return a query where all the unique page numbers are returned with the max version number of each page.
Here is the an example of the data that I'd query
DocumentID PageNumber Version
1 1 1
1 2 1
1 2 2
1 3 1
1 3 2
1 3 3
And here is what I would need to get returned in my query
DocumentID PageNumber Version
1 1 1
1 2 2
1 3 3
Not sure how to finish this:
var pages = from p in dc.Pages where p.DocumentID == 1 && ...

I think this is what you're trying to achieve:
var results =
from p in dc.Pages
where p.DocumentID == 1
group p by p.PageNumber into g
select new
{
PageNumber = g.Key,
MaxVersion = g.Max(x => x.Version)
};

This query may help you:
Select DocumentID ,Distinct PageNumber, max(version) from table
group by DocumentID, Distinct PageNumber

Related

Count rows from 1 table that have more than x rows in another table

I have 3 tables - Folders, Documents & Versions
FolderID
Folder Name
1
Folder 1
2
Folder 2
3
Folder 3
Documents looks like this:
DocID
Doc Name
FolderID
1000
Doc 1
1
1001
Doc 2
1
1002
Doc 3
2
1003
Doc 4
2
1004
Doc 5
3
Versions looks like this:
VersionID
DocID
1
1000
2
1001
3
1001
4
1002
5
1003
6
1003
7
1004
So Doc 1, 3 & 5 have 1 version each, and Doc 2 & 4 have 2 versions.
I would like to count the documents that have more than 1 version. In this example Folder 1 & 2 both have 1 document with more than 1 version, and Folder 3 has none.
I'd like some DAX that will accomplish that. I'm managing to confuse myself because the filter is based on a count of a related table.
This is what I came up with, but I know I'm off
Count Docs =
VAR VersionsMin = 2
RETURN
CALCULATE (
COUNT ( 'Documents'[DocID] ),
FILTER ( 'Versions', COUNT ( 'Versions'[VersionID] ) >= VersionsMin )
)
Try this out:
In the first step add a new column to your Documents table
Version Count =
COUNTROWS(RELATEDTABLE(Versions))
In the second step you can use this column for filtering
Docs with multiple versions =
CALCULATE(
COUNT(Documents[DocID]),
Documents[Version Count] > 1
)
This allows you to create the following table visual:

Hierarchical query get all children as rows

Data:
ID PARENT_ID
1 [null]
2 1
3 1
4 2
Desired result:
ID CHILD_AT_ANY_LEVEL
1 2
1 3
1 4
2 4
I've tried SYS_CONNECT_BY_PATH, but I don't understand how to convert it result into "inline view" which I can use for JOIN with main table.
select connect_by_root(id) id, id child_at_any_level
from table
where level <> 1
connect by prior id = parent_id;

How do I create a ID based on multiple values in different columns in Power Query?

I am trying to create an ID based in multiple values in different columns in Power query.
The idea is to check the following values:
IF
ID_STORE = 1
ID_PRODUCT = 1
ID_CATEGORY = 1
SALE_DATE = 01/01/2018
ID_COSTUMER = 1
THEN CREATE THE SAME ID FOR THE ROWS THAT HAVE THIS INFO.
The idea is to check the rows that have that info (1 and 01/01/2018) in multiple columns (ID_STORE, ID_PRODUCT, ID_CATEGORY etc..).
Thanks in advance.
Obs: This is my first post, so feel free to correct me in any manner.
You need to add an 'AND' clause to your 'OF' statement;
if [ID_STORE] = 1
and [ID_PRODUCT] = 1
and [ID_CATEGORY] = 1
and [SALE_DATE] = Text.ToDate("01/01/2018")
and [ID_COSTUMER] = 1 then
1 else 0

not getting output in group by using linq query

My table is:
Student_answer_Master:
Stud_ans_id QuestionId OptionId iscorrect
1 25 1 1
2 26 1 0
3 27 2 1
4 27 3 1
5 27 4 1
6 28 1 1
8 29 1 0
9 29 2 1
10 29 3 1
(my options are single choice and multiple choice(checkbox)).
now what i want to get total count of only those question which are correct.
1-indicate correct
0-indicate incorrect.
i want output:3(for questionid 25,27,28)
this is my linq query:
var data = (from temp in context.Student_Answer_Master
where temp.isCorrect == '1'
group temp by temp.Question_Id into g
select new {g.Key}).Count();
but here count is coming 0.
can any one tell me what is wrong with my query???
Try this this should work.
var data = (from temp in s
group temp by temp.Question_Id into g
select new { questionid = g.Key, minIscorrect = g.Min(x => x.isCorrect) }).Where(y => y.minIscorrect != 0).Count();
There are 2 things that looks wrong to me in your Query.
comparing an int to a char temp.isCorrect == '1' if you make it temp.isCorrect == 1, you will still get 4 as the count because you are eliminating the row that has wrong answer for Question id 29.
once you get the group, you need to check if there are any wrong answers for the question
if yes eliminate that and then put a count

Where returns object reference not set to an instance of object

I'm new to Linq. I've 2 tables Table 1 and Table 2. They are related by Id1.
Table 1 Table2
------- ------
Id1 Id2 RId1 (reference key from TAble 1)
1 1 1
2 1
2 3 2
4 2
5 Null
3 6 .
. 7 .
.
When I use the Where clause to query data from table 2, I get error Object reference not set to an instance of object
var result = db.Table2.Where(i => i.Rid1 == 1);
Even this code doesn't help
if (result != null)
Please help me.
In this case it appears that Rid1 is a nullable. Comparing a null against an integer may result in that error. Try changing it to the following:
db.Table2.Where(i => i.Rid1.GetValueOrDefault(0) == 1);

Resources