linq for records not having a value - c#

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

Related

How to Update Column based on another table column value

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

How to do select multiple records based on max version using Entity Framework

I have records such like this
id name number version
---------------------------
1 NewYork 1 1
2 LosAngeles 1 2
3 Seatle 1 3
4 Toronto 2 1
5 Ottawa 2 2
I want to select only the records with highest version within the same number
So I wrote query like this
SELECT *
FROM city c
WHERE c.[version] = (SELECT Max([version])
FROM [city] c2
WHERE c2.number = c.number)
and it would return
id name number version
---------------------------
3 Seatle 1 3
5 Ottawa 2 2
How do I write that in linq with Entity Framework?
db.cities.where(c => c.version == (????))
I don't know how Entity Framework would work for this.
Use GroupBy to group by the number and then OrderBy the record with the highest version:
var result = db.cities.GroupBy(item => item.number)
.Select(grouping => grouping.OrderByDescending(item => item.version)
.First());
var maxVersionCities = db.cities
.GroupBy(c => c.number)
.Select(grp => grp
.OrderByDescending(c => c.version)
.First())
.SelectMany(grp => grp);

LINQ group by on a single column

I have a database table that looks like this:
ID USERID DATE NAME
1 1 01-01-2001 aaaa
1 2 01-02-2001 aaaa
1 3 01-03-2001 aaaa
2 5 02-02-2002 bbbb
2 6 02-02-2002 bbbb
2 7 02-02-2002 bbbb
So I want to group everything by ID, so it has multiple USERIDs for each ID. However DATE and NAME are all the same. So how do get this as a linq query?
So far I thought of doing this, and this works for the users
from table in context.table
group table.USERID by table.ID into grp
orderby grp.Key descending
select new
{
ID = grp.Key,
Users = grp.ToList()
}
But this does not work if I add other items like DATE because DATE is sometimes different. However if it would select the first date that would be just fine.
EDIT what I would like as result is:
ID Users DATE NAME
1 1,2,3 01-01-2001 aaaa
2 5,6,7 02-02-2002 bbbb
You group by multiple columns:
from table in context.table
group new{table.UserID,table.Date} by new{table.ID,table.Name} into grp
orderby grp.Key.ID descending
select new
{
ID = grp.Key.ID,
Date=grp.FirstOrDefault().Date,
Name=grp.Key.Name,
Users = grp.Select(e=>e.UserID).ToList()
}

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

SELECT UNTIL in SQL with Entity Framework

I would like to select rows until I found a certain Id which is numeric. If my data is ordered by Id the problem can be solved quite easy.
Id Name
-----------
1 Bob
2 Eve
3 Alice
4 Michael
5 Anne
6 Mike
To get all items until Id 4 is found the following SQL statement is sufficient:
SELECT * FROM Users WHERE Id <= 4
If the data is ordered by Name and I still would like to get the items until Id 4 is found I couldn't come up with a good solution.
Id Name
-----------
3 Alice
5 Anne
1 Bob
2 Eve
4 Michael
6 Mike
The output of the statement to be defined should be:
Id Name
-----------
3 Alice
5 Anne
1 Bob
2 Eve
4 Michael
EDIT 1:
With the following statement the output is almost what I need but missing the item with Id 4.
queryable.OrderBy(o => o.Name).TakeWhile(o => o.Id != 4);
Id Name
-----------
3 Alice
5 Anne
1 Bob
2 Eve
Is it possible to include the item with Id 4 too?
EDIT 2:
For now I'm going with this approach:
queryable.OrderBy(o => o.Name).TakeWhile(o => o.Id != 4).Union(queryable.Where(o => o.Id == 4))
As the Id is unique there should be no problem with the UNION statement. But I'm not sure if this statement is the most effective one.
Context.Table.OrderBy(p => p.Name).TakeWhile(p => p.id != 4);
var result = Users.TakeWhile((u,i)=> i == 0 || Users.ElementAt(i-1).Id != 4);
Another approach:
var result = Users.TakeWhile(u=>u.Id!=4)
.Union(Users.SkipWhile(u=>u.Id!=4).Take(1));
var results = from n in names.TakeWhile( n => n.Id != 4)
.Union(names.Where(n => n.Id == 4)) select n;

Categories