C
C#•6mo ago
DevDream

Generic Method Type Parameter to create DbSet for my Database query?

I want to make my code reusable by passing a type parameter to a generic method and using that parameter to create a Dbset for my Database Query. The method I want to pass it to is GetUnPagedList(). I'm using .NET Core 7 for this project. Thank you for reading.
7 Replies
Jimmacle
Jimmacle•6mo ago
where exactly are you stuck? use the Set<T> method on your context to get a dbset with a generic type
Angius
Angius•6mo ago
Does every model have a relationship to ApplicationUser? And, similarly, CreationDate? If not, you would have to start by declaring those in an interface And constraining the generic to that interface Or, what I like to do, use an extension method on IQueryable<IThatInterface> instead
DevDream
DevDream•6mo ago
@🅱immacle Im stuck on GetUnPagedList method and converting it to a Generic Method to even pass the Entity @ZZZZZZZZZZZZZZZZZZZZZZZZZ Yes let me post my DB Context and Model one moment Database Context File Post Model Identity User Model
Angius
Angius•6mo ago
Seems like only Post has .ApplicationUser property
DevDream
DevDream•6mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ Yeah only 2 of my DB models have a relationship with Application User
Angius
Angius•6mo ago
So, I'd start with an interface Then, constrain your method's generic
private IEnumerable <T> GetUnpagedList<T>() where T : IWhatever
private IEnumerable <T> GetUnpagedList<T>() where T : IWhatever
Then use context.Set<T>() to get the DbContext of a given type
DevDream
DevDream•6mo ago
Thank You! You guys are awesome for the lightning fast replies I owe you one Solved by @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Want results from more Discord servers?
Add your server
More Posts
Compile .NET project with nuget packages Visual StudioI want to create .DLL file out of dotnet project, but it uses some Nuget dependencies, which I wouldtetromino is phasing through my grid in unityits alot of stuff so i will just post a reddit link here where I put my infomation https://www.reddiHow to assign roles to already created Users in ASP.NET Identity?In ASP.NET I am trying to attribute Roles to specific users in my Database, I already have the users✅ how do I read a json file, edit it and save the edit in the json file?lets say we have a json file here: ```json { "meow" : 2 }``` 3 things Im wondering how to do(code bIs there a standard option to generate UUID version 7? (Guid)Hello, .NET provides a way to generate UUID v4 with `Guid.NewGuid()`, but this cannot be used as a What is the up-to-date version of AspNet.WebAPI?I am upgrading my console application to include Web Api, so i changed the sdk from `Microsoft.NET.S[WPF] How to edit multiple items in ListBox at onceI'm trying to edit multiple items using SelectionMode="Extended" to give the items a specific value,Please Help me. I spent 3 days trying to fix it to no avail.I am very new to coding and this is my first project. Trying to fix it but I couldnt manage it. httErrors while trying to build a VSA templatehttps://github.com/viceroypenguin/VsaTemplate/ I got errors while trying to build this templateSerialize value of property onlyGiven this code: https://dotnetfiddle.net/C3Pt3C How would I only serialize the value of the "TempT