using group by in IEnumerable result - c#

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

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

Get all the variables of a SQL Dataset that are the max of a sort order using LINQ

I have data in a single database table like so:
Id NameId EndNumber SortOrder
1 TestA 1 1
1 TestA 3 2
1 TestA 9 3
1 TestB 1 1
1 TestB 2 2
1 TestB 4 3
1 TestB 6 4
1 TestB 7 5
1 TestC 2 1
1 TestC 4 2
1 TestC 6 3
I have a LINQ query like this:
result = _getService.QueryWithNoTracking<GetNamesId>()
.Where(q => q.Id == myId && names.Contains(q.NameId)
I want to my select to return this:
Id NameId EndNumber
1 TestA 9
1 TestB 7
1 TestC 6
I essentially want the max sort order value of each id/nameId combination. I understand the select but not sure if/how to use an OrderBy to achieve this.
You'd probably use groupby for this, rather than OrderBy
result = _getService
.QueryWithNoTracking<GetNamesId>()
.Where(q => q.Id == myId && names.Contains(q.NameId))
.GroupBy(gni => gni.NameId)
.Select(g =>
new {
Id = g.First().Id,
NameId = g.Key,
EndNumber = g.Max(gni => gni.EndNumber)
}
);
LINQ grouping is slightly different to sql. It takes a single list of items and produces a "list of lists" where every item in the inner list has the same property that was specified for key. By grouping on NameId you get a list of lists, which you then turn back into a single list by throwing away most of the EndNumbers in the inner lists and just keeping the max value

llbl gen group by with conditional sum could not bound an Error

I am using linq query with llblgen and it's give me an error, anyone can please help me to resolved this issue. This is my query
var reuslt = from cc in _metadata.TableName
where cc.StdId == 125
group cc by new { cc.Column1, cc.Column2 } into gr
select new
{
Col1 = gr.Key.Column1,
Col2 = gr.Key.Column2,
IncomeType1 = gr.Sum(x => x.TypeId == 1 ? x.MonthlyIncome : 0),
IncomeType2 = gr.Sum(x => x.TypeId == 2 ? x.MonthlyIncome : 0)
};
And Error that occuring in this query when i do result.ToList()
Error:
The multi-part identifier "LPLA_3.TypeId" could not be bound.
The multi-part identifier "LPLA_3.MonthlyIncome" could not be bound.
This is data
Id stdId column1 column2 TypeId MonthlyIncome
1 125 1234 12 1 4
2 125 1235 12 2 4
Expected Output should be
StdId column1 column2 TypeId1 TypeId2 IncomeType1 IncomeType2
125 1234 12 1 2 4 4
please help me

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

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

Categories