Hi I have a linq query which I use to get data from the DB.
And I have join two tables in the query.
Here is my database structure..
I need to get Customer with primary telephone number and default shipping address.
And this is my query..
var customer=UnitOfWork.DbContext.Set<Domain.BoundedContext.ScreenPop.Entities.Customer>()
.Include(x => x.CustomerPhoneNumbers.Select(p => p.IsPrimary == true))
.Include(x => x.ShippingAddresses.Select(s => s.IsDefault == true))
.Where(c => c.CustomerId == customerQuery.CustomerId).FirstOrDefault();
But it gives me this error..
The Include path expression must refer to a navigation property defined on the type.
Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
How can I get these information by using those three tables
The includes are just a way to say what navigation properties/tables you want included. Try this
var customer=UnitOfWork.DbContext.Set<Domain.BoundedContext.ScreenPop.Entities.Customer>()
.Include(x => x.CustomerPhoneNumbers)
.Include(x => x.ShippingAddresses)
.Where(c => c.CustomerId == customerQuery.CustomerId).FirstOrDefault();
and then just get the phone number and address you want
var phonenumber = customer.CustomerPhoneNumbers.FirstOrDefault(x=>x.IsPrimary);
var address = customer.ShippingAddresses.FirstOrDefault(x=>x.IsDefault);
Related
I am trying to find all users based on a specific role in my application. I currently have two roles Admin and User. When I try to return a list of users that have a role of "User" with the following linq query:
var users = context.Users
.Where(u => u.Roles.Select(r => r.RoleId).Contains("User")).ToList();
it returns 0 users. I have looked at similar questions here but many of them are now outdated. I know that RoleID for example is a hashed key and what I am searching for is plain text "User".
RoleId is not the same as the actual role name string, so that's why you're not getting any matches. The way you need to do this is:
var roleId = context.Roles.Where(m => m.Name == "User").Select(m => m.Id).SingleOrDefault();
Then:
var users = context.Users
.Where(u => u.Roles.Any(r => r.RoleId == roleId)).ToList();
i have an online store with dynamic fields (specifications) and filters. All specifications are stored in table e.g
product_specifications:
-id
-product_id
-spec_id
-spec_val_id
Filters are connected with specifications, so when i send filter value i send specification values. For example
$_GET['Filters']['filter_id']['spec_val_id']
When i loop filters, i want to add left joins to product query. Something like that
$joinString = "";
foreach($filters){
$joinString .= "LEFT JOIN product_specifications AS prod_'.filter.' ON .....";
}
I have query to ActiveDataProvider like that:
$prodQuery = Product::find()->joinWith('translation')->joinWith('cats')->[HERE FILTERS JOINS]->where($whereString)->groupBy(['id']);
But if i have 5 filters, i need 5 joins to table product_specifications.
Can i add in joinWith an array with all joins or add 5 joins to query chain?
To one category my filters are also dynamically, so the page can have 5 or 10 filters, i can't add static number of joinWith('specs').
Thanks in advance.
I'm agree with different desicion, too.
EDIT:
I change query with findBySql like this
$prodQuery = Product::findBySql('
SELECT `product`.*
FROM `product`
LEFT JOIN `productLang` ON `product`.`id` = `productLang`.`product_id`
LEFT JOIN `product_cat` ON `product`.`id` = `product_cat`.`product_id`
LEFT JOIN `page` ON `product_cat`.`page_id` = `page`.`id`
LEFT JOIN `page` `parent` ON `page`.`id_in` = `parent`.`id`'
.$joinString.
' WHERE ( '
.$whereString.
' AND (`language`=\''.$lang->url.'\')) GROUP BY `product`.`id` ');
And my dataprovider:
$dataProvider = new ActiveDataProvider([
'query' => $prodQuery,
'pagination' => [
'totalCount' => count($prodQuery->all()),
'pageSize' => $pageSize,
'route' => Yii::$app->getRequest()->getQueryParam('first_step'),
],
'sort' => [
'defaultOrder' => $orderArr,
],
]);
Now my problem is pagination and sorting. In my test site i have 4 products, when i set $pageSize = 1;, I have 4 pages in pagination, but in every page i have all 4 products.
Where is my mistake?
Could be you can use andFilterWhere (if the value is null no where codntion is added otherwise the and Where is added)
$prodQuery = Product::find()->joinWith('translation')->joinWith('cats')->where($whereString)->groupBy(['id']);
$prodQuery->andFilterWhere(['attribute1' => $value1])
->andFilterWhere(['attribute2' => $value2])
->andFilterWhere(['attribute3' => $value3])
->andFilterWhere(['attribute4' => $value4])
->andFilterWhere(['attribute5' => $value5])
I have implemented ASP.Net identity with some custom properties following this article -
http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx
Everything works well, except. I want to get users under specific role (e.g. Get me all the users under Admin role).
I tried following ways to retrieve the users -
var userRole = _roleManager.Roles.SingleOrDefault(m => m.Name == role.Name);
var usersInRole = _userManager.Users.Where(m => m.Roles.Any(r => r.RoleId == userRole.Id));
var usersInRole2 = _userService.GetUsers().Where(u => u.Roles.Any(r => r.RoleId == userRole.Id));
Where _roleManager is of type ApplicationRoleManager : RoleManager<ApplicationRole>. _userManageris of type ApplicationUserManager : UserManager<ApplicationUser, string>.
I am unable to get Roles under user in _userManager and _userService
PS : _userService is service that extends IRepository which queries DbSet<ApplicationUser>.
I can see Roles being properly mapped in table ApplicationUserRoles and I get expected result when I do _userManager.IsInRole(user.Id, "Admin");.
What could've gone wrong with this?
Rahul.
If you are using Entity Framework, it sounds like you are being caught out by lazy loading (since the roles are being added to the database but not when requested from a queryable).
Try something like the following:
_userManager.Users.Include(x => x.Roles).Where(m => m.Roles.Any(r => r.RoleId == userRole.Id));
I figured out where the issue was -
Initially the table ApplicationUserRoles had only primary key definitions, not the foreign key mapping (many to many mapping)..
I added this in OnModelCreating
modelBuilder.Entity<ApplicationUserRole>().HasKey((ApplicationUserRole r) => new { UserId = r.UserId, RoleId = r.RoleId });
//added these definitions
modelBuilder.Entity<ApplicationUser>().HasMany(p => p.Roles).WithRequired().HasForeignKey(p => p.UserId);
modelBuilder.Entity<ApplicationRole>().HasMany(p => p.Users).WithRequired().HasForeignKey(p => p.RoleId);
This completed the relationship and now I can see the Users under Roles and vice versa.
This resulted issue while updating the database, however I just had to do some changes in migration -
The object 'PK_Dbo.ApplicationUserRole' is dependent on column
'UserId'. ALTER TABLE DROP COLUMN UserId failed because one or more
objects access this column.
All I did is, I went to the migration file and moved these lines above DropColumn
DropIndex("dbo.ApplicationUserRole", new[] { "ApplicationUser_Id" });
DropIndex("dbo.ApplicationUserRole", new[] { "ApplicationRole_Id" });
DropPrimaryKey("dbo.ApplicationUserRole");
This solved the update-database exceptions as well.
Rahul
Lets say I have two models with a many to many relationship: Item and Property
Now I have an array of properties and I want to filter all items which properties match a given value (lets say a boolean value: property.value = true)
When I try
#items = Item.includes(:properties).where(:properties => {:id => [1,2,3].to_a, :value => true})
I would like to get all items where property(1) is true AND property(2) is true and so on. But with the code above I get all items related to the property id's and where any property is true. How should I change my code?
I would appreciate not to use a gem for this.
Looks like you are almost there:
property_ids = [1,2,3]
Item.joins(:properties).
where(:properties => { :id => property_ids, :value => true }).
group('items.id').
having('COUNT(properties.id) >= ?', property_ids.size)
joins does an INNER JOIN and is preferred over includes when you really need to join tables.
where is basically the conditions you already had, the only change is that there is not need to call to_a on the array.
Than you have to group to make that COUNT in SQL work.
having extracts the lines that have at least the expected number of property lines matching the condition.
I'm trying to build a search query using NHibernate that will filter on parameters from several different tables and result in somewhat-reasonable SQL that can take advantage of NHibernate's lazy-loading.
Fromm reading various tips online, it seems that the latest-and-greatest way to do that is to use the QueryOver object to conditionally add in the parameters being used, as in the following snippet:
Hibernate.Criterion.QueryOver<Models.Site, Models.Site> query = NHibernate.Criterion.QueryOver.Of<Models.Site>();
if (!string.IsNullOrEmpty(state))
query = query.WhereRestrictionOn(r => r.State.StateName).IsInsensitiveLike("%" + state + "%");
if (startDate.HasValue)
query = query.Where(r => r.Events
.Where(e=>e.EventDate >= startDate.Value)
.Count() > 0
);
return query.GetExecutableQueryOver(currentSession).Cacheable().List();
(an event has a foreign-key to site)
I have two questions:
How do I filter the child objects, instead of just the parent? The sample code above gives me all the sites with a matching events, but within that site, I only want matching events. If I'm supposed to use a join or a subquery, how? I'm confused about maintaining my tree-like hierarchy with lazy-loading through a join or subquery.
Edit: this has been answered. Thanks psousa!
How do I add an or clause? I found reference to a Disjunction object, but it doesn't seem like that's available using the QueryOver method.
Edit:
I want to result in a list of sites (top level object) filtered by the site criteria, and each site should have its list of events filtered by the event criteria.
I expect it to generate SQL like the following:
SELECT *
FROM [site] s
LEFT JOIN [event] e ON s.siteID = e.siteID
WHERE e.eventDate > #eventDate
AND (s.stateCd = #state OR s.stateName LIKE #state)
I would do that query as such:
//use aliases. Optional but more practical IMHO
Site siteAlias = null;
Event eventAlias = null;
//use JoinAlias instead of JoinQueryOver to keep the condition at the "Site" level
var results = Session.QueryOver(() => siteAlias)
.JoinAlias(m => m.Event, () => eventAlias)
.Where(() => eventAlias.EventDate > eventDate)
.Where(() => siteAlias.StateCd == state || Restrictions.On(() => siteAlias.StateName).IsLike(state))
.List();
You mentioned the Disjunction class, and it may in fact be used with QueryOver, like:
var disjunction= new Disjunction();
disjunction.Add(() => siteAlias.StateCD == state);
disjunction.Add(Restrictions.On(() => siteAlias.StateName).IsLike(state));
The QueryOver query would be:
var results = Session.QueryOver(() => siteAlias)
.JoinAlias(m => m.Event, () => eventAlias)
.Where(() => eventAlias.EventDate > eventDate)
.Where(disjunction)
.List();
When using a join alias as suggested by psousa, you will get results in a strange combination of an object structure and a row structure, with the top-level objects being duplicated by the child objects that are attached to them. In order to get the results I was looking for, you can use TransformUsing and a DistinctRootEntityResultTransformer as shown in the following code:
Site siteAlias = null;
Event eventAlias = null;
var results = currentSession.QueryOver<Site>(() => siteAlias)
.JoinAlias(m => m.Event, () => eventAlias)
.Where(() => eventAlias.EventDate > eventDate)
.Where(() => siteAlias.StateCd == state || Restrictions.On(() => siteAlias.StateName).IsLike(state))
.TransformUsing(new NHibernate.Transform.DistinctRootEntityResultTransformer())
.List();