How to use multiple condition in LINQ joins, i.e. in my scenario I need to get all the users from table User where group ID = 4 from table UserInGroup, where UserInGroup is intermediate table between User and Group table as in SQL-T we use join as
select *
from user
where user.userID = userIngroup.userID AND userIngroup.groupID == 4
....
In another approach I am using lambda expression along with LINQ, how I can apply where groupID = 4 in following one??
public IEnumerable<User> GetUsersByGroupID(int _groupID)
{
List<User> _listedUsersByGroupID = new List<User>();
using(var _uow = new UserManagement_UnitOfWork())
{
_listedUsersByGroupID = (from _users in _uow.User_Repository.GetAll()
.Include(s=>s.UserInGroup.Select(r=>r.Group))
select _users).ToList();
return _listedUsersByGroupID;
}
}
User Model
[Table("User")]
public class User
{
public User() { }
[Key]
public int UserID { get; set; }
[StringLength(250)]
[Required]
public string FirstName { get; set; }
[StringLength(250)]
[Required]
public string LastName { get; set; }
[Required]
public int Age { get; set; }
[StringLength(250)]
[Required]
public string EmailAddress { get; set; }
public ICollection<UserInGroup> UserInGroup { get; set; }
}
UserInGroup Model
[Table("UserInGroup")]
public class UserInGroup
{
public UserInGroup() { }
[Key]
public int UserGroupID { get; set; }
[Required]
public int UserID { get; set; }
[Required]
public int GroupID { get; set; }
public User User { get; set; }
public Group Group { get; set; }
}
Group Model
public class Group
{
public Group() { }
[Key]
public int GroupID { get; set; }
[StringLength(250)]
[Required]
public string GroupName { get; set; }
public ICollection<UserInGroup> UserInGroup { get; set; }
}
You only need to add a condition to filter the users that belong to the group 4. Try this:
_listedUsersByGroupID = (from _user in _uow.User_Repository.GetAll()
.Include(s=>s.UserInGroup.Select(r=>r.Group))
where user.UserInGroup.Any(ug=>ug.groupID==4)
select _user).ToList();
Lambda query would look something like:
ctx.User.Where(user=>
ctx.UserInGroup.Any(userIngroup=>
user.userID == userIngroup.userID && userIngroup.groupID == 4
)
)
That however is just query, if you want to get results add .AsList() or .AsEnumerable() to end.
However you can write silly and inefficient code if you do not fully understand what you are doing. I would reccomend you try this instead:
var publications = ctx.Database.SqlQuery<UserResults>(String.Format(#"
select UserID, FirstName,LastName,Age,EmailAddress,UserInGroup
from user
where user.userID = userIngroup.userID AND userIngroup.groupID == {0}
order by UserID
", Config.Group));
Where Config.Group is 4; UserResults can be User table as well, if you do not want other fields. You need to execute or enumerate over the sql query to use the data and like before you can use .AsList() or .AsEnumerable() for that.
Variable ctx is database context. For example:
using (var ctx = new toxicEntities())
{
}
Related
My classed are declared as follow:
[Table("Profil")]
public class Profil
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column("CodeUtilisateur")]
[MaxLength(25)]
public string UtilisateurId { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
[ForeignKey("UtilisateurId")]
public virtual User User { get; set; }
}
[Table("vwADUser")]
public class User
{
[Key]
public string UserName { get; set; }
[Column("Mail")]
public string Email { get; set; }
}
Here's my linq query.
var query = from profil in icDbContext.Profils
select new
{
profil.Nom,
profil.Prenom,
profil.UtilisateurId,
UserEmail = profil.User != null ? profil.User.Email : ""
};
var result = query.ToList();
The query that is sent to the database produce an inner join with the view [dbo].[vwADUser].
SELECT
1 AS [C1],
[Extent1].[Nom] AS [Nom],
[Extent1].[Prenom] AS [Prenom],
[Extent1].[CodeUtilisateur] AS [CodeUtilisateur],
[Extent2].[Mail] AS [Mail]
FROM [ic].[Profil] AS [Extent1]
INNER JOIN [dbo].[vwADUser] AS [Extent2] ON [Extent1].[CodeUtilisateur] = [Extent2].[UserName]
I have also tried this query with same result.
var query = from profil in icDbContext.Profils
join user in icDbContext.Users on profil.UtilisateurId equals user.UserName into gusers
from guser in gusers.DefaultIfEmpty()
select new
{
profil.Nom,
profil.Prenom,
profil.UtilisateurId,
UserEmail = guser != null ? guser.Email : ""
};
var result = query.ToList();
I tried to manually configure the relationship with a few variants of this command without success.
modelBuilder.Entity<Profil>()
.HasOptional(x => x.User)
.WithOptionalDependent();
Any idea on how to configure my relationship to produce a LEFT JOIN instead of an INNER JOIN?
Try this instead:
[Table("Profil")]
public class Profil
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column("CodeUtilisateur")]
[MaxLength(25)]
public string UtilisateurId { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public virtual User User { get; set; }
}
[Table("vwADUser")]
public class User
{
[Key]
[ForeignKey("Profil")]
public string UserName { get; set; }
[Column("Mail")]
public string Email { get; set; }
public virtual Profil Profil {get;set;}
}
This is how you would do it Fluently:
modelBuilder.Entity<Profil>()
.HasOptional(x => x.User) // User is optional for Profil
.WithRequired(z=>z.Profil); // but Profil is required for User
I'm trying to get all time entries for a specific foreman based on his supervisor's supervisor. However, I seem to be having trouble writing a self join query in ORMLite. See my data structure and code below.
public class User
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
[References(typeof(User))]
public int SupervisorId { get; set; }
}
public class TimeSheet
{
[AutoIncrement]
public int Id { get; set; }
[References(typeof(User))]
public int ForemanId { get; set; }
}
var query = db.From<TimeSheet>()
.Join<User>()
.Join<User, User>(); // not sure how to write this one.
// .Where(super => super.SupervisorId = 2)
I've created a sample gist to try and better show what I've attempted.
var query = db.From<TimeSheet>()
.Join<User>()
.Join<User, User>((p, q) => p.Id == q.SupervisorId, db.JoinAlias("u2"));
I am trying to get away from the Entity Framework since I have to support HANA Databases aside from SQL server Databases in our solution.
I am doing some research with dapper so I created a quick test environment with some fictitious scenario.
I have the following POCOs that resemble my Database schema (I have more but I limited to showing these for simplicity):
public class Adopter
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public State State { get; set; }
public int StateId { get; set; }
public string Zip { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public IEnumerable<Pet> Pets { get; set; }
}
public class State
{
public int Id { get; set; }
public string Name { get; set; }
public string Abreviation { get; set; }
}
public class Pet
{
public int Id { get; set; }
public string IdTag { get; set; }
public string Name { get; set; }
public DateTime AdmitionDate { get; set; }
public Status Status { get; set; }
public int StatusId { get; set; }
public string Notes { get; set; }
public DateTime AdoptionDate { get; set; }
public bool IsAdopted { get; set; }
public int? AdopterId { get; set; }
public int Age { get; set; }
public decimal Weight { get; set; }
public string Color { get; set; }
public Breed Breed { get; set; }
public int BreedId { get; set; }
public Gender Gender { get; set; }
public int GenderId { get; set; }
public IEnumerable<PetImage> PetImages { get; set; }
}
public class Status
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Gender
{
public int Id { get; set; }
public string Name { get; set; }
}
I am using the following in a repository to return a list of all the adopters:
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var adopters = connection.Query<Adopter>("SELECT a.* FROM Adopters a");
foreach (var adopter in adopters)
{
adopter.State = connection.QueryFirst<State>("Select s.* FROM States s WHERE s.Id = #Id", new { Id = adopter.StateId });
adopter.Pets = connection.Query<Pet>("Select p.* FROM Pets p WHERE p.AdopterId = #Id", new { Id = adopter.Id });
foreach (var pet in adopter.Pets)
{
pet.Status = connection.QueryFirst<Status>("Select s.* FROM Status s WHERE s.Id = #Id", new { Id = pet.StatusId });
pet.Gender = connection.QueryFirst<Gender>("Select g.* FROM Genders g WHERE g.Id = #Id", new { Id = pet.GenderId });
}
}
return adopters;
}
As you can see, I am retrieving the data for each POCO individually based on the previous one and doing the Joins manually in code.
Is this the right way of doing it or should I be doing a big query with multiple joins and mapping the result somehow thru dapper and LINQ?
A possible improvement to your actual solution is through the use of QueryMultiple extension like this:
using (SqlConnection connection = new SqlConnection(_connectionString))
{
string query = #"SELECT * FROM Adopters;
SELECT * FROM States;
SELECT * FROM Pets;
SELECT * FROM Status;
SELECT * FROM Genders;";
using (var multi = connection.QueryMultiple(query, null))
{
var adopters = multi.Read<Adopter>();
var states = multi.Read<State>();
var pets = multi.Read<Pet>();
var statuses = multi.Read<Status>();
var genders = multi.Read<Gender>();
foreach (Adopter adp in adopters)
{
adp.State = states.FirstOrDefault(x => x.Id == adp.StateID);
adp.Pets = pets.Where(x => x.IsAdopted &&
x.AdopterID.HasValue &&
x.AdopterID.Value == adp.AdopterID)
.ToList();
foreach(Pet pet in adp.Pets)
{
pet.Status = statuses.FirstOrDefault(x => x.Id == pet.StatusID);
pet.Gender = genders.FirstOrDefault(x => x.Id == pet.GenderID);
}
}
}
}
The benefit here is that you reach the database just one time and then process everything in memory.
However this could be a performance hit and a memory bottleneck if you have a really big data to retrieve, (and from a remote location). Better to look closely at this approach and try also some kind of Async processing and/or pagination if possible.
I don't like to be negative, but... don't do this! Don't even think like this. You want to dump EF, but you're walking into the trap by wanting to emulate EF. The bridge between your app and your DB is not something to be built once for all time, for every conceivable purpose. Concretely, you shouldn't really ever bring back a whole table, and certainly not to then loop on every row and emit more queries. You may feel unjustly criticised, you were just testing the tools ! If so, perhaps tell us what aspect of the tool your examining, and we'll focus in on that.
Dapper or QueryFirst greatly simplify running queries, and consuming the results, so bring back just what you need, just when you need it. Then denormalize a little, for the specific job in hand. Why are there no joins in your queries? RDBMSs are amazing, and amazingly good at doing joins. If you're joining data outside the DB, crazy is the only word, even if Linq gives you a super (sql-like) syntax for doing it. The unthinking assumption that 1 table corresponds to 1 class is the start of a lot of problems.
I have this scenario
public class TimeSheet
{
[Key]
public int TimeSheetID { get; set; }
public string Username { get; set; }
}
public class Approval
{
[Key]
public int ApprovalID { get; set; }
[Index(IsUnique = true)]
[StringLength(450)]
public string Approver { get; set; }
public virtual ICollection<ApprovalDetail> Details { get; set; }
}
public class ApprovalDetail
{
[Key]
public int ApprovalDetailID { get; set; }
[StringLength(450)]
public string Username { get; set; }
}
I want to the following syntax in EF.
SELECT
*
FROM
TimeSheet
WHERE
UserName IN (SELECT
[AD].Username
FROM
Approval [A]
INNER JOIN
ApprovalDetail [AD] ON [A].ApprovalID = [AD].ApprovalID
WHERE
[A].Approver = 'warheat1990')
How to achieve this?
UPDATE :
My Repo
public IEnumerable<TimeSheet> List()
{
return _timeSheet.AsEnumerable().ToList();
}
public IEnumerable<TimeSheet> ListByUsername(string username)
{
return _timeSheet.Where(w => w.Username == username).ToList();
}
This should do it:
var usernamesByApprover = approvals
.Where(a => a.Approver == "warheat1990")
.SelectMany(a => a.Details.Select(d => d.Username));
var timesheetsByApprover = timesheets
.Where(t => usernamesByApprover.Contains(t.Username));
Note that even if the query is split into two expressions, Entity Framework will convert it into a single SQL query once you evaluate the timesheetsByApprover variable because of deferred execution.
I am displaying a record from my database. The record pulls data from other tables and uses a Int in the main table to represent the value so Item table has a Division equal to 1 and the Division table 1 = ALL . Now that i am displaying the records i am trying to turn the 1 into all. All the ID fields show the int. Which is what my code is telling it to do. But i am trying to display the name and when i do that i get a lot of red. It cannot find the name. CatagoryID should be CategoryName.
Hope that makes sense.
if (!IsPostBack)
{
string v = Request.QueryString["ContactID"];
int itemid;
int.TryParse(v, out itemid);
var customerInfo = GetCustomerInfo(itemid);
CONTACTID.Text = customerInfo[0].ContactID.ToString();
ContactTitle.Text = customerInfo[0].ContactTitlesID.ToString();
ContactNameB.Text = customerInfo[0].ContactName;
DropDownAddCategory.Text = customerInfo[0].CategoryID.ToString();
DDLAddDivision.Text = customerInfo[0].DivisionID.ToString();
ContactPhoneBox.Text = customerInfo[0].ContactOPhone;
ContactCellBox.Text = customerInfo[0].ContactCell;
ContactEmailBox.Text = customerInfo[0].ContactEmail;
CextB.Text = customerInfo[0].Ext;
}
private List<Solutions.Models.Contact> GetCustomerInfo(int itemid)
{
using (ItemContext context = new ItemContext())
{
return (from c in context.Contacts
where c.ContactID == itemid
select c).ToList();
}
}
This is the model
public class Contact
{
[ScaffoldColumn(false)]
public int ContactID { get; set; }
public System.DateTime ContactCreated { get; set; }
public string ContactName { get; set; }
public int? ContactTitlesID { get; set; }
public string ContactOPhone { get; set; }
public bool cApproved { get; set; }
public string User { get; set; }
public string ContactCell { get; set; }
public string ContactEmail { get; set; }
public int? DivisionID { get; set; }
public int? CategoryID { get; set; }
[StringLength(5)]
public string CExt { get; set; }
public virtual Division Division { get; set; }
public virtual Category Category { get; set; }
public virtual ContactTitle ContactTitle { get; set; }
public string Ext { get; set; }
}
With Entity Framework you can include related entities in query results:
return (from c in context.Contacts.Include("Catagory")
where c.ContactID == itemid
select c).ToList();
This will return contacts with Catagory objects: customerInfo.Catagory.CategoryName
BTW instead of returning list of contacts and selecting first one by index (thus possibly having index out of range exception), modify your method to return first contact (or default, if not found):
private Solutions.Models.Contact GetCustomerInfo(int itemid)
{
return (from c in context.Contacts.Include("Catagory")
where c.ContactID == itemid
select c).FirstOrDefault();
}
And use it this way:
var customerInfo = GetCustomerInfo(itemid);
if (customerInfo != null)
{
CONTACTID.Text = customerInfo.ContactID.ToString();
// etc
}
Are you using LINQ to SQL or Entity Framework? Check your model again and make sure the relationship between the two tables are setup correctly. The relationship may be missing from the model, and causing this problem.