LINQ and Lambda, query based on an array - sql

I have this model and I want to write a where clause that query specific results based on and array, for example I want to show only the song that has an id in the array [1, 3, 7, 8]:
I wrote the expression below but I don't know how to write the where statement:
var model = from c in _db.Categories
from co in _db.Composers
from k in _db.Keys
from p in _db.Poets
from si in _db.Singers
from t in _db.Types
join s in _db.Songs on
new
{
Catid = c.id,
Comid = co.id,
Keyid = k.id,
Poetid = p.id,
Singerid = si.id,
Typeid = t.id
}
equals
new
{
Catid = s.CategoryId,
Comid = s.ComposerId,
Keyid = s.KeymId,
Poetid = s.PoetId,
Singerid = s.SingerId,
Typeid = s.TypeId
}
where
............
select new SongViewModel
{
id = s.id,
Name = s.Name,
Lyric = s.Lyric,
Chord = s.Chord,
Note = s.Note,
Audio = s.Audio,
Lycho = s.Lycho,
Likes = s.Likes,
Dislikes = s.Dislikes,
Category = c.Name,
Composer = co.Name,
Keym = k.Name,
Poet = p.Name,
Singer = si.Name,
Type = t.Name
};

Try this:
var ids = new List<int> {1,3,7,8};
...
where ids.Contains(s.Id)
select
...

Related

How to join linq in c#?

How to convert sql to system.linq?
Select top 100 percent s.a,s.b,s.c,s.d
From table a as s, table b as x
Where
s.a=x.a and s.b=x.b and s.c=x.c
Group by
s.a,s.b,s.c,s.d
As per my understanding of your question; seems like you want to fetch the data in c# and do joining. if so, then you may do as following:
public class tabData
{
public string a {get;set;}
public string b {get;set;}
public string c {get;set;}
public string d {get;set;}
}
List<tabData> tabA = {data of your table a}
List<tabData> tabB = {data of your table b}
var result = from r1 in tabA
join r2 in tabB on new {T1 = r1.a, T2 = r1.b, T3 = r1.c} equals new {T1 = r2.a, T2 = r2.b, T3 = r2.c}
group r1 by new
{
aa = r1.a,
bb = r1.b,
cc = r1.c,
dd = r1.d
} into g
select new
{
a = g.key.aa,
b = g.key.bb,
c = g.key.cc,
d = g.key.dd
}
I think you are asking how to join in linq as you would in sql, if so, please see below:
var query =
from abc in tbl1
join def in tbl2 on tbl1.PK equals tbl2.FK
select new { ABC = abc, DEF = def };

select some ids from sql in linq

hi I'm using the query below to select studentId and Score from table1 now i want select users that i selected their ids from table2, how i can select it with ids?
i can select users with this query from v in dc.tbl_Students select v but i want select some users that i have their id.
var qBestMan = (from T in (((from tbl_ActPoints in dc.tbl_ActPoints
select new
{
StudentId = (int?)tbl_ActPoints.StudentId,
Score = (int?)tbl_ActPoints.Score
}).Concat(
from tbl_EvaPoints in dc.tbl_EvaPoints
select new
{
StudentId = (int?)tbl_EvaPoints.StudentId,
Score = (int?)tbl_EvaPoints.Score
})))
group T by new
{
T.StudentId
} into g
orderby g.Sum(p => p.Score) descending
select new
{
g.Key.StudentId,
HighScoreUser = g.Sum(p => p.Score)
}).ToArray();
Try something like this:
//qBestMan must be a List, or a IEnumarable and not a Array. Remove the .ToArray() at the end, or substitute it by .ToList()
var Result = from users in dc.tbl_Students
join bestMen in qBestMan on bestMen.StudentId equals users.userid
select new
{
//fields that you want
example = users.example,
other = bestMen.other
};

SQL to LINQ query equivalent

Hello I need to crate linq query from this SQL:
SQL:
select
p.id,
p.Name,
sum(h.Hour)
from
dbo.Hour h
INNER JOIN dbo.ProjectView p ON h.ProjectId = p.Id
WHERE
h.PeopleId = 7999
group by
p.Name, p.Id
LINQ: I tried this but it is not the same:
var query = from hours in _hourRepository.GetAll()
join proj in _projectRepository.GetAll() on hours.ProjectId equals proj.Id
where hours.PeopleId == personId
group hours by new { proj.Id, proj.Name, proj.Flag, hours.Hours } into g
select new PopleProjectsSumDto
{
Id = g.Key.Id,
Name = g.Key.Name,
Flag = g.Key.Flag,
Hours = g.Sum(h => h.Hours)
};
OK i find solution by removing hours from group:
LINQ:
var query = from hours in _hourRepository.GetAll()
join proj in _projectRepository.GetAll() on hours.ProjectId equals proj.Id
where hours.PeopleId == personId
group hours by new { proj.Id, proj.Name, proj.Flag } into g
select new PopleProjectsSumDto
{
Id = g.Key.Id,
Name = g.Key.Name,
Flag = g.Key.Flag,
Hours = g.Sum(h => h.Hours)
};

Linq Sum based on some columns

I have a query returning some values for a specific CompanyId and a Specific Month/Year.
I want to make a report for the whole year. (So I need to sum up the values from different months, but of the same year and CompanyId)
Here is my query now:
from er in EconomicReports
join com in Companies on er.CompanyId equals com.Id
join cou in Countries on com.CountryId equals cou.Id
where er.Year == 2014
select new
{
Country = cou.Name,
CompanyId = com.Id,
CorporationId = com.CorporationId,
Year = er.Year,
RegisteredCases = er.NewCasesTotalCount,
RegisteredCasesAmount = er.NewCasesTotalAmount,
ResolvedCases = er.ClosedCasesTotalCount,
ResolvedCasesAmount = er.ClosedCasesCapitalAmount + er.ClosedCasesInterestAmount,
ActiveCases = (er.NewCasesTotalCount ?? 0) - (er.ClosedCasesTotalCount ?? 0),
ActiveCasesAmount = (er.NewCasesTotalAmount ?? 0) - (er.ClosedCasesCapitalAmount ?? 0) - (er.ClosedCasesInterestAmount ?? 0)
}
Basically, rows 1 and 5 need to be one row, because they are from the same year, same company Id (I have put also Month in the results for you to see that is a different month, but same year)
you need a GroupBy:
var query = (from er in EconomicReports
join com in Companies on er.CompanyId equals com.Id
join cou in Countries on com.CountryId equals cou.Id
where er.Year == 2014
select new
{
Country = cou.Name,
CompanyId = com.Id,
CorporationId = com.CorporationId,
Year = er.Year,
RegisteredCases = er.NewCasesTotalCount,
RegisteredCasesAmount = er.NewCasesTotalAmount,
ResolvedCases = er.ClosedCasesTotalCount,
ResolvedCasesAmount = er.ClosedCasesCapitalAmount + er.ClosedCasesInterestAmount,
ActiveCases = (er.NewCasesTotalCount ?? 0) - (er.ClosedCasesTotalCount ?? 0),
ActiveCasesAmount = (er.NewCasesTotalAmount ?? 0) - (er.ClosedCasesCapitalAmount ?? 0) - (er.ClosedCasesInterestAmount ?? 0)
});
var result = query
.GroupBy(u => new {u.Country, u.CompanyId, u.CorporationId, u.Year})
.Select(u => new
{
Country = u.Key.Country,
CompanyId = u.Key.CompanyId,
CorporationId = u.Key.CorporationId,
Year = u.Key.Year,
RegisteredCases = u.Select(t => t.RegisteredCases).DefaultIfEmpty().Sum(),
RegisteredCasesAmount = u.Select(t => t.RegisteredCasesAmount).DefaultIfEmpty().Sum(),
ResolvedCases = u.Select(t => t.ResolvedCases).DefaultIfEmpty().Sum(),
ResolvedCasesAmount = u.Select(t => t.ResolvedCasesAmount).DefaultIfEmpty().Sum(),
ActiveCases = u.Select(t => t.ActiveCases).DefaultIfEmpty().Sum(),
ActiveCasesAmount = u.Select(t => t.ActiveCasesAmount).DefaultIfEmpty().Sum()
})
.ToList();
i think Country and CorporationId must be same for grouped records based on CompanyId and Year, so i was included them in group by, to use in Select

SQL to LINQ Group by convertion

I have an SQL statement and I want to convert it to LINQ. The problem now is that I don't know how to group by it in LINQ.
Here is the code.
In SQL
select plp.ProspectsListID, p.Prospect_PII_Key
from ProspectListProspect plp
join Prospects p
on p.ProspectsID = plp.ProspectsID
group by plp.ProspectsListID,p.Prospect_PII_Key
In LINQ
var list1 = from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
join p in GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
on plp.ProspectsID equals p.ProspectsID
select new
{
ProspectID = plp.ProspectsListID,
Prospect_PII_Key = p.Prospect_PII_Key
};
thanks
Jason
tyr this
var list1 = from item in
(
from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
join p in GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
on plp.ProspectsID equals p.ProspectsID
select new
{
ProspectID = plp.ProspectsListID,
Prospect_PII_Key = p.Prospect_PII_Key
}
)
group item by new {item.ProspectID ,item.Prospect_PII_Key } into grp
select new
{
ProspectID = grp.ProspectsListID,
Prospect_PII_Key = grp.Prospect_PII_Key
}
;
Check this
var list1 = from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
join p in GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
on plp.ProspectsID equals p.ProspectsID
Group By Key = New With {plp.ProspectsListID,p.Prospect_PII_Key} Into Group
Select Group;
var list1 = from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
join p in GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
on plp.ProspectsID equals p.ProspectsID
group p by new {plp.ProspectsListID,p.Prospect_PII_Key} into g
select new
{
ProspectID = g.Key.ProspectsListID,
Prospect_PII_Key = g.Key.Prospect_PII_Key
};