Write a LINQ query for 2 tables - c#

I have got 2 tables, Student and CourseTaken. I need to write a LINQ code that displays all CourseTaken, that has Active student status set as true.
I wrote part of the LINQ statement that will display all CourseTaken for a particular Id. How can I further filter it by showing the coursetaken for Active students? (S_ID in CourseTaken contains the student Id.)
List<CourseTaken> courseTakenList =
await dbcont
.CourseTaken
.Where(c => c.CId == courseId)
.ToListAsync();
public class Student
{
public int Id;
public string Name;
public string School;
public bool Active;
}
public class CourseTaken
{
public int CId;
public string CourseName;
public int S_Id;
}
Note: I need to use LINQ and Lambda expressions.

This will give you a list of all courses that has an active student, this assumes you have a navigation property from courses to student called Students
var result = dbcont.CourseTaken.Where(c => c.Students.Any(s => s.Active));
If this is not correct, i think you need to explain your structure better, whether this is Entity framework and you have the appropriate navigation property, and some example data
Update
No, I don't have navigation properties in place. Is there another way
I could get this done ?
Well you probably should, as you are going to have to query the database twice now.
var ids = dbcont.Students.Where(s => s.Active)
.Select(x => x.id)
.ToList();
var result = dbcont.CourseTaken.Where(c => ids.Contains(c.S_Id));
Lastly, take a look at a few entity framework tutorials, your column naming is a little weird, and you really need to hook this up in the spirit of EF. with navigation properties

It sounds to me that you need this query:
from ct in dbcont.CourseTaken
where ct.CId == courseId
join s in dbcont.Student.Where(s => s.Active) on ct.S_Id equals s.Id into gsc
where gsc.Any()
select ct
This is only returning a CourseTaken once, regardless of how many active students are taking the course, as long as their is at least one, of course.

int[] StudentsId =( from s in dbcont.Students
where s.Active ==true
select s.Id).ToArray<int>();
List<CourseTaken> courseTakenList = dbcont.CourseTaken.
Where(c=> StudentsId.Contains(c.S_Id) )
.ToList();

var result =
(from C in db.CourseTakens
join S in db.Students.Where(s => s.Active == true) on C.S_Id equals S.Id
select C
).ToList();
This can get only CourseTaken data. You can add Student data to select clause.

Related

Filtering Related Entites with Entity Framework

According to this StackOverflow answer:
Linq to Entities - how to filter on child entities
you should be able to filter down the list of related entities in Entity Framework by utilizing a projection, like I've done here:
Company company = _context.Company
.Where(g => g.CompanyId == id)
.Select(comp => new
{
group = comp,
operators = comp.Operator,
formFamilies = comp.FormFamily.Where(ff => ff.IsActive ?? false)
}).AsEnumerable().Select(i => i.group).FirstOrDefault();
To give a quick overview of what I'm trying to obtain here, I'm trying to get a list of all of the active form families associated with this company object, however, whenever I restrict the results in any way, the result set is empty.
If the line were formFamilies = comp.FormFamily then it returns two results, one active one inactive
If the line is formFamilies = comp.FormFamily.Where(ff => true) then it returns nothing
If the line is formFamilies = comp.FormFamily.OrderBy(ff => ff.FormFamilyId) then it returns nothing.
Any sort of modification that I do to comp.FormFamily means the result set returns nothing, I've dug through the deepest sections of SA to try to find a solution, and tried every solution I've found, but nothing seems to cause this list to return anything.
Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.
var company = from c in _context.Company
join f in _context.FormFamily
on c.Id equals f.CompanyId
where c.Id == id
select new Company()
{
Id = c.Id,
operators = c.Operator.ToList(),
formFamilies = c.FormFamily.Where(x=>x.IsActive ==
false).ToList()
} .FirstOrDefault();
Hope this helps.
I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.
Anyway, you could simply use Include methods instead of projecting.
var company = _context.Company
.Where(c => c.Id == id)
.Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
.ToList();
Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.

NHibernate 3.3 Right Join QueryOver One-to-One Relationship

So, this is the situation. I have a C# project that uses NHibernate 3.3 (upgrading is not an option), with the following classes:
public class Person{
public List<PersonAddress> PersonAddresses {get;set;}
}
public class NaturalPerson : Person{
public NaturalProperty NaturalProperty {get;set;}
}
public class LegalPerson : Person{
public LegalProperty LegalProperty {get;set;}
}
public class Quarantine{
public Person QuarantinePerson {get;set;}
public QuarantineProperty QuarantineProperty {get;set;}
}
What I need to do is to obtain Persons that may or not "be in quarantine" and that satisfy different conditions that involve Quarantine's properties, and legal or natural properties. Basically what I need is a Left Join from Persons, or a Right Join from Quarantines:
Select *
From Quarantine q
Right Join Persons p on p.ID = q.QuarantinePersonID
Left Join NaturalPersons np on p.ID = np.ID
Left Join LegalPersons lp on p.ID = lp.ID
Where q.Property = 1
and np.Property = 1
This is what I have now. I have managed to do the "from" clause that I need, but I'm having serious problems with the "select" and "where" clauses:
Person p = null;
Quarantine q = null;
var results = this.Session.QueryOver<Quarantine>(() => q)
.JoinQueryOver<Person>(() => q.QuarantinePerson, () => p, JoinType.RightOuterJoin)
.SelectList(list => list
.Select(() => p.Id))
.TransformUsing(Transformers.AliasToBean<Person>())
.List<Person>();
This code generates the following query:
`SELECT p1_.PersonID as y0_
FROM BUP.Quarantines this_
right outer join BUP.Persons p1_
on this_.QuarantinePersonID = p1_.PersonID
left outer join BUP.LegalPersons p1_1_
on p1_.PersonID = p1_1_.PersonID
left outer join BUP.NaturalPersons p1_2_
on p1_.PersonID = p1_2_.PersonID`
As I said, the "from" clause is OK. Now the problem is with the Select and Where clauses.
The main problem with the Select is that I need the whole Person object, wether it's a legal or a natural person. I have tried removing the .SelectList from the query, but it throws a PropertyNotFoundException: "Could not find a setter for property 'p' in class 'Person'".
And the problem with the Where clause is that i don't know how to add conditions based on NaturalPerson and LegalPerson's properties. I have no problem filtering Quarantine and Person properties, but haven't yet succedeed to do the same with the other classes.
Also, this query should be as performant as possible, because timeout is a serious problem. I have managed to do other solutions with subqueries and such, but they took too long.
Any help, with any of the two issues, will be very appreciated!
Thanks!!
You can use subqueries mixing with future to execute both queries in one row trip:
var subquery = QueryOver.Of<Quarantine>()
.Select(x => x.QuarantinePerson.Id);
var naturalPersons = Session.QueryOver<NaturalPerson>()
.WithSubquery.WhereProperty(x => x.Id).NotIn(subquery)
//.Where(x => x.NaturalProperty == somehing)
.Future();
var legalPersons = Session.QueryOver<LegalPerson>()
.WithSubquery.WhereProperty(x => x.Id).NotIn(subquery)
//.Where(x => x.LegalProperty == somehing)
.Future();
var persons = naturalPersons.Cast<Person>().Union(legalPersons);

Obtain more information from the user via Inner join Linq

I supposed in the process developed is such that it must show all the movies that are into film tablen and showing off, but this is how I have tried to do this:
it must find out which genres have in users tablen where after to show the users who like the first.
//As I said, I have a session at the top of the code.
int brugerid = Convert.ToInt16(Session["id"]);
var result = (from f in db.films
//it must find out which genres have in users tablen where after to show the users who like the first.
//brugere are users
//gener It is the genes users like.
join usersgenerId in brugere.Fk_generId on gener.generId equals usersgenerId.BrugereId
select new
{
image_navn = ((f.imgs.FirstOrDefault(i => i.feature == true)).navn == null ? "default.png" : (f.imgs.FirstOrDefault(i => i.feature == true)).navn),
image_feature = f.imgs.Where(A => A.feature == true),
film_navn = f.navn,
film_id = f.filmId,
film_tekst = f.tekst,
film_gener = f.gener.navn
}).ToList();
RepeaterFilmList.DataSource = result;
RepeaterFilmList.DataBind();
Table information
Brugere the name
id = BrugereId
Fk_generId belonging to the genes that user has selected.
and many other
Gener is the name
has generId as id
As mentioned in the comment, the question really is: show all movies that is in the same genre that the user preferred and then show everything else.
Although the following approach might not be db efficient (too lazy to create the db for this, so I am simulating everything in memory and using Linq to Object to solve the issue), it can certainly be resolved by the following steps:
Get the recommendation (matching the user's movie genre preference) like so:
var recommendation =
from f in films
from ug in userGenres
where ug.UserId == user.Id && ug.GenreId == f.GenreId
select f;
Now that we know what the user preferred, we can further filter this to just the preferred films' Id... and use that to get the rest of the unpreferred films (basically anything not matching the preferred film Ids):
var recommendedFilmIds = recommendation.Select(f => f.Id);
var everythingElse =
from f in films
where !recommendedFilmIds.Contains(f.Id)
select f;
Finally, join them together using Union and injecting the nessary fields for display purpose like Genre.Name, etc. like so:
var filmList = recommendation.Union(everythingElse).Select(f => new {
f.Id,
f.Title,
Genre = genres.Where(g => g.Id == f.GenreId).Select(g => g.Name).First()
});
And there you have it, the combined list will now contains both preferred films first (at top), followed by unpreferred films afterward.
The simulated tables are as follows: films which contains its own Id and genreId and userGenres which contains many to many relationship between user and genre and a particular user object which contains the user id.
An example of this can be found at: https://dotnetfiddle.net/Skuq3o
If you use EF, and you have a navigation property to genre table and you want to include those table as part of the query, use .Include(x => x.genre) or whatever you call your genre table after from f in films to avoid n+1 select if you wish to include the genre info in the final select clause.

C# - Entity Framework - Join method

I have a table called "PublicUserOfferingSignUp" which contains the following columns.
Id
PublicUserId - foreign key to PublicUser.Id
OfferingId
Created
My application is using Entity Framework, but I am getting stuck with how to join from the PublicUserOfferingSignUp table to the PublicUser table.
I want to obtain a list of PublicUserOfferingSignUp records but ordered by the Name column of the PublicUser table.
Currently I have this ....
return DataBase.PublicUserOfferingSignUps.Join(PublicUser,
But I can't seem to work it out, any ideas ....
Steven
Can anybody help.
Something like that
DataBase.PublicUserOfferingSignUps.Join(Database.PublicUsers,
puosu => puosu.PublicUserId,//PublicUserOfferingSignUps key
pu => pu.Id,//PublicUser key
(puosu, pu) => new {
publicUsersOfferingSignUp = puosu,//we take all data from PubliUserOfferingSignUps
puName = pu.Name//and Name from PublicUser
})
.OrderBy(x => x.puName)//we order by PublicUser Name
.Select(x => x.publicUsersOfferingSignUp );//we take only the PublicOfferingSignUps
Edit : as #M.Schenkel noticed, it would be easier to have a
public virtual PublicUser PublicUser {get;set;}
in your PublicUserOfferingSignUp model
then the query would be
DataBase.PublicUserOfferingSignUps
.OrderBy(puosu => puosu.PublicUser.Name);
easier, no ?
When you use the Entity Framework, the public user should be a property of your PublicUserOfferingSignUp-entity. If not, you can write a LINQ query to join them. For example:
var result = from pu in context.PublicUserOfferingSignUp
join u in context.PublicUser on u.id equals pu.PublicUserId
select pu;
(this code is untested, but should give you the idea).

Get distinct records using linq to entity

Hi I'm using linq to entity in my application. I need to get distinct records based on one column value "Name"
So I have a table similar like you can see below:
(User)
ID
Name
Country
DateCreated
I need to select all this items but uniques based on Name (unique). Is it possible to accomplish using linq, if so please show me how.
var items = (from i in user select new {i.id, i.name, i.country, i.datecreated}).Distinct();
The Distinct() method doesn't perform well because it doesn't send the DISTINCT SQL predicate to the database. Use group instead:
var distinctResult = from c in result
group c by c.Id into uniqueIds
select uniqueIds.FirstOrDefault();
LINQ's group actually creates subgroups of entities keyed by the property you indicate:
Smith
John
Mary
Ed
Jones
Jerry
Bob
Sally
The syntax above returns the keys, resulting in a distinct list. More information here:
http://imar.spaanjaars.com/546/using-grouping-instead-of-distinct-in-entity-framework-to-optimize-performance
The purely LINQ way that occurs is to group by name, select distinct groups by key, then select based on that.
from i in user
group new {i.ID, i.Country, i.DateRecord} by i.Name into byNmGp
select byNmGp.First();
Edit: Entity Framework is of course a very popular linq provider, but it doesn't handle First() well here, though the logically equivalent (in this case) FirstOrDefault() will work fine. I prefer First() when not forced into FirstOrDefault() by EF's limitations, because its meaning better matches what is sought here.
Another way is to define a helper class:
private class MyRecord : IEquatable<MyRecord>
{
public int ID;
public string Name;
public string Country;
public DateTime DateCreated;
public bool Equals(MyRecord other)
{
return Name.Equals(other.Name);
}
public override bool Equals(object obj)
{
return obj is MyRecord && Equals((MyRecord)obj);
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
/*...*/
var items = (from i in user select new MyRecord {i.ID, i.Name, i.Country, i.DateRecord}).Distinct();
This simply defines distinct differently. Performance will differ by whether the query provider can interpret that definition of equality or not. Convenience will differ based on whether you've similar LINQ queries doing much the same thing or not.
You can use something like this:
var distinctReports = reports.Select(c => c.CompanyCode)
.Distinct()
.Select(c => reports.FirstOrDefault(r => r.CompanyCode == c))
.ToList();
Here's another variation I ended up using which was based off the response from Svetlana. Shows an example of populating a GridView control with unique values. Thanks!
dataGridView_AnalyzeTestSuites.DataSource = (
from tr in _db.TestResults
where tr.TaskId == taskId
select new { TestSuiteName = tr.Test.TestSuite.Name }
).Distinct().ToList();
Hi here is how you can select distinct records with inner join. Hope it helps
var distinctrecords =
(entity.Table.Join(entity.Table2, x => x.Column, y => y.Column, (x, y) => new {x, y})
.Select(#t => new {#t.x.Column2, #t.y.Column3}))
.GroupBy(t => t.Column2)
.Select(g => g.FirstOrDefault());

Categories