How to Update Column based on another table column value - c#

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

Related

linq for records not having a value

I have table with values
Table Event
Id UserId EventId
===========================
1 1 1
2 1 2
3 1 3
4 2 2
Table Users
Id FirstName LastName
===========================
1 xx xx-last
2 ww ww-last
3 dd dd-last
4 qq qq-last
I want to have the list of users which forexample don't have EventId=1. in this case it would be
User 2,3,4
how can i make it with linq
Assuming that your User entity has an Events navigation property:
var usersWithoutEvent1 = dbContext.Users
.Where(u => u.Events.All(e => e.Id != 1))
.ToArrayAsync();

Linq left join with multiple tables

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...

using group by in IEnumerable result

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();

EF Query Help - Grouping and Sub queries

I have data table (DOCs, which is the DBSet in my context) with below data
ID Code Rev
1 A1 1
2 A1 2
3 A1 3
4 A3 1
5 A2 1
6 A2 2
I need to select the records which has a records for each Code which has the highest Rev. My expected result is
ID Code Rev
3 A1 3
6 A2 2
4 A3 1
The ID column is the PK of the table and Code+Rev is unique.
Note: There are other fields in the table which i need to get for the result. Ideal would be to get a iqueryable (Doc is the model class), i was think of selecting the ID within an inner query and then use that to get the iqueryable of docs.
Thanks in Advance
Try this:
var res = from r in DOCs
where (from c in DOCs
group c by c.Code into g
select new {
localCode = g.Key,
localRev = g.Max(t => t.Rev)
}).Any(x => x.localCode == r.Code && x.localRev == r.Rev)
select r;
res is IQueryable.

Linq Query for EAV design table

I had a situation to use EAV design tables and I am new to this design,I am struck with a select query. Below is my query structure and data.
TABLE1:
Id KeyName
1 Name
2 Age
TABLE2:
ID TABLE1_ID VALUE
1 1 ABC
2 2 12
3 1 CDF
4 2 14
5 1 XYZ
6 2 13
7 1 CSF
8 2 10
EXPECTED OUTPUT: Get all the values which are greater than 12 AND Value contains "C".
i.e.,
Table2_ID Result Table1_KeyName
1 ABC Name
2 12 Age
3 CDF Name
4 14 Age
Options I tried are:
Var temp = (from c in Table2
where c.Value > 12 && c.Table1.KeyName.Contains("C")
Select new
{
ID = c.ID,
Result = C.Value
});
the above query didn't returned any result, as filters(in where clause) are across rows. I even tried "OR" condition in where clause, it returns me everything. Please do help me.
Your query is wrong, did you type it in here or copy and paste it from your work?
Do you have this table setup in an ORM such as Entity Framework? So that there is a relationship setup between table1 and table2, so you don't have to 'join' them?
To be more 'correct' it should be...
Var temp = (from c in Table2
where c.Value > 12 && c.Table1.Name.Contains("C")
Select new
{
ID = c.ID,
Name = c.Table1.Name,
Value = c.Value
});
Or if the relationship isn't in an ORM your using explicitly specify the join as follows:
Var temp = (from c in Table2
join c1 in Table1 on c.Table1_ID equals c1.Id
where c.Value > 12 && c.Table1.Name.Contains("C")
Select new
{
ID = c.ID,
Name = c.Table1.Name,
Value = c.Value
});
Edit: then it should be an OR, not AND
Var temp = (from c in Table2
where c.Value > 12 || c.Value.Contains("C")
Select new
{
ID = c.ID,
Result = c.Value,
KeyName = c.Table1.Name
});

Categories