Hi I am developing web application in .net. I have come across with below scenario.
Below is my first table.
perm_levelid scrn_id perm_id perm_read perm_write
40 1 2 1 1
41 2 2 1 1
42 3 2 1 1
Below is my second table.
scrn_id scrn_name
1 UserProfile
2 Change Password
3 Dashboard
4 Lease request
The relation between above two table is scrn_id
The output i am expecting is
scrn_id perm_id perm_read perm_write
1 2 1 1
2 2 1 1
3 2 1 1
4 0 0 0
I want total 4 rows in the result. scrn_id 1,2,3 matching in both tables so i should retrieve first table perm_read and perm_write values. Remaining value from 2 nd table also i want to get.
I tried as below.
List<screendetails> obj = new List<screendetails>();
obj = (from c in db.rolsp_perm_levelmapping
join mapdetails in db.rolsp_scrn_screen on c.scrn_id equals mapdetails.scrn_id
into mapObj
from wt in mapObj.DefaultIfEmpty()
where c.perm_id== permisssionID
select new screendetails {
scrn_id=c.scrn_id,
scrn_name= wt.scrn_name,
Read=c.perm_read,
Write=c.perm_write
}).ToList();
return obj;
Above query does not yields correct result. May i get some help here to fix this? Thank you
obj= (from scrn in db.rolsp_scrn_screen
from rmap in db.rolsp_perm_levelmapping
.Where(rl=>rl.scrn_id==scrn_id).DefaultIfEmpty() select new screendetails {
scrn_id=scrn.scrn_id,
scrn_name= scrn.scrn_name,
Read=rmap.perm_read,
Write=rmap.perm_write
}).ToList();
Try using this... Hope this helps...
Related
I have two data tables
table
id isfav
---------
1 1
2 1
3 1
4 1
5 1
favtable id
-----------
2 true
3 false
So I want to update the table1 column isFav to 0 if the ids exist in FavTable with false.
Can anybody help me with this?
You can use Any() to search in other entities.
var db = new YourDbContext();
var favtable = db.favtable.ToList();
//Find them:
var result = db.Table1.Where(t => favtable.Any(f => f.id == t.id && !f.isfav));
//result should be 3.
.NET Fiddle: https://dotnetfiddle.net/BmaqN5
I have a query like below :
IEnumerable<qryTable1> templates = from t in db.qryTable1
where (t.GID == 1&&
t.RID == 4 && t.CID == "user")
select t;
Let say, Above query returns following result:
TId GId RId CID
1 1 1 a
1 1 2 a
2 1 1 a
2 1 2 a
3 1 1 a
3 1 2 a
Now I want to get result from the above query like below:(removing RId column so that result have duplicate entries having only TId,GId,CID and then group the result by TId).
TId GId CID
1 1 a
2 1 a
3 1 a
Also , I want to get the desired output in template object only.It means result should be in 'IEnumerable<qryTable1>' object.
var distinct = db.qryTable1.Select(r => new {Tid = r.TId, GId = r.GId, CId = r.CId})
.Distinct();
I'm stuck with this linq query.
I've this tables.
ID A B C D
1 some data
2 some other data
Then, For every record on that table I may have none or many rows
ID TableA_ID R
1 1 1
2 1 2
3 1 5
4 2 2
For example. Row 1 (some data) has 3 rows on table B.
I tried using
tableA.Include(x => x.tablebchilds.Where( d => d.R == 1)).ToList()
but it is not working. With many others varation.
The objective of this query is to return tableA.row #1 if I pass it 1 as value (value of R). Number <> 2 won't give any result.
Tables are linked on EF. So TableB.tableA_ID is Foreign key of tableA.ID
Edit #1
I tried the answers in the question marked as duplicated with no luck. Give that 2 tableA.rows if the user insert 1 as parameter, linq query should return Row #1, some data. If 2 is passed as parameter, nothing is return.
A working SQL statement is:
SELECT [TableA].* FROM [TableA] JOIN [TableB] ON [TableA].[Id] = [TableB].[TableA_Id] WHERE [TableB].[R] = 1
Thanks!
If you have database relationship properly configured this have to work.
tableA.Include(x => x.tableBChilds).Where(tableA => tableA.tableBChilds.Any(b => b.R== 1)).ToList();
I know this question has already been asked many times.
The problem is that other solutions don't work..
I tried the following:
SELECT ID FROM TABLE_1 AS T1 WHERE NOT EXISTS (SELECT COLUMN_1,COLUMN_2 FROM TABLE_2 AS T2 WHERE T1.COLUMN_1 = T2.COLUMNS_1 AND T1.COLUMN_2 = T2.COLUMN_2);
It always go in timeout, both from Workbench and from code (I am using Visual Studio 2013 C#).
I don't know how to make the query easier in order to make it work.. Maybe split it in 2..
Example:
Table 1 Table 2
ID COLUMN_1 COLUMN_2 ID COLUMN_1 COLUMN_2
1 0 1 1 0 1
2 0 1 2 0 1
3 0 1 3 0 1
4 1 2
5 1 2
6 1 2
It should return
1 2
Or also only the ID (2).
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
LEFT JOIN Table_2
ON Table_1.ID = Table_2.ID
AND Table_1.COLUMN_1 = Table_2.COLUMN_1
AND Table_1.COLUMN_2 = Table_2.COLUMN_2
WHERE Table_2.ID IS NULL
Edit:
Well, if you don't need to match the id, then it's simply:
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
LEFT JOIN Table_2
ON Table_2.COLUMN_1 = Table_1.COLUMN_1
AND Table_2.COLUMN_2 = Table_1.COLUMN_2
WHERE Table_2.ID IS NULL
If that's still too slow, maybe an index can help.
If an index doesn't help, you can still increase the command timeout.
Still, another option would be:
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
WHERE
(COLUMN_1, COLUMN_2) NOT IN (SELECT COLUMN_1, COLUMN_2 FROM Table_2)
I have the following table
CamId RegNumber DateSeen
5 G1234B 18/02/2014 11:54
3 G1234B 18/02/2014 11:51
5 G11854 18/02/2014 11:50
3 G11854 18/02/2014 11:49
3 G24581 18/02/2014 11:48
5 G24581 18/02/2014 11:47
I would like to retrieve all records with CamId 3 that do not have a later entry in CamId 5 based on the dateseen entry on CamId 3 for that particular reg number.
From the sampe data table above the system should return just one number plate G24581 as it has no later entries in CamId 5.
One method you can employ is to join onto the table twice so that in one table you have the values where CamID is 3 and the other is 5. Then you would want to get all records from the first table that do no have a record in the second table.
Select A.* from Table A
LEFT JOIN ( Select * from Table ) B on A.RegNumber = B.RegNumber AND A.CamID = 3 and B.CamID = 5 AND A.DateSeen <= B.DateSeen
WHERE B.CamID IS NULL
Try this:
var query = from r1 in db.Regs
where r1.CamId == 3 &&
!db.Regs.Any(r2 => r2.CamId==5 && r2.DateSeen>r1.DateSeen && r2.RegNumber==r1.RegNumber)
select r1;