I have two classes :
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Product> Product { get; set; }
}
public class Product
{
public string ProductNumber { get; set; }
public string ProductColor { get; set; }
}
I want to create a clause where on property Product (Product.ProductColor == "") I do :
c.Where(x => x.Product.????? == "11").Select(x => x).ToList();
How do this ?
I assume you want to find customers, that have a product with Number 11. If so, you can use function Any:
var result = c
.Where(x => x.Product.Any(p => p.ProductNumber == "11"))
.ToList();
The code filters only those customers, that have at least one product that satisfies condition ProductNumber == "11"
Or if you want to find customers that have specific color then use different expression:
var result = c
.Where(x => x.Product.Any(p => p.ProductColor == "Color"))
.ToList();
Since Product (which really should be named Products) is also a collection, you'd have to drill down into that collection. For example, if you want all Customers from a list of customers where any product color is "11", it might looks like this:
customers.Where(c => c.Product.Any(p => p.ProductColor == "11"))
Related
I'm having some problem with my linqToEntities query. The Product is missing in the query result. Is there any way to return the ProductQuantity with the Product property correctly with a linqToEntities expression?
public class ProductQuantity
{
public string Id { get; set; }
public string SomeProperty { get; set; }
public Product Product { get; set; }
public Guid ProductId { get; set; }
}
public class Product
{
public Guid Id { get; set; }
public string SomeProperty { get; set; }
//...
}
// MyId is the ProductId I need
// The following will return all productQuantity detail but the Product property will be null
var result = myEntities.ProductQuantities.Include(x => x.Product).Where(x => x.ProductId == MyId)
// The following will work but I want to avoid refilling the object like this :
var result = myEntities.ProductQuantities.Include(x => x.Product).Where(x => x.ProductId == MyId)
.Select(y => new ProductQuantity{ SomeProperty = y.SomeProperty, Product = y.Product});
What is the proper way to do this with linq to entities? Why the product is not just simply returned with the query ?
Thanks
EDIT 1
Look like my problem is releated to .Include() when using more than one include.
Just add a Category to ProductQuantity in the preceding example :
//This will return the product but not the category
var result = myEntities.ProductQuantities.Include(x => x.Product).Include(x=> x.Category).Single(x => x.ProductId == MyId)
//This will return the category but not the product
var result = myEntities.ProductQuantities.Include(x => x.Category).Include(x=> x.Product).Single(x => x.ProductId == MyId)
Why only one include can be used and only the first one is working??????? (a saw tons of similar example on the net?)
Any help?
Seems like there is a problem when the same entity is used in any other include. (ex: Product.Unit and Product.AlternateUnit cannot be retreived at the same time if the same entity is used ie:unit) I dont really understand why but I use separate query to fetch the data that cannot be retrieved by the include.
How to write 'Where Any In' in LINQ to Entity?
Here is my model :
class Chair
{
public int Id { get; set; }
public int TableId { get; set; }
public Table Table { get; set; }
}
class Table
{
public int Id { get; set; }
public ICollection<Chair> Chairs { get; set; }
public ICollection<Category> Categories { get; set; }
public Table()
{
Chairs = new List<Chair>();
Categories = new List<Category>();
}
}
class Category
{
public int Id { get; set; }
public ICollection<Table> Tables { get; set; }
}
I also got a simple list of Category :
List<Category> myCategories = new List<Category>(c,d,e);
I want to get only that Chairs that belongs to Table that got one of the Category from myCategories List. Thats what im trying to do :
var result =
ctx.Chairs.Where(x => x.Table.Categories.Any(y => myCategories.Any(z => z.Id == y.Id))).ToList();
I think its ok but what i get is error :
"Unable to create a constant value of type 'ConsoleApplication1.Category'. Only primitive types or enumeration types are supported in this context"
Try to compare with in-memory categories Ids collection, instead of categories collection.
var myCategoriesIds = myCategories.Select(c => c.Id).ToArray();
var result =
context.Chairs
.Where(
x => x.Table.Categories.Any(
y => myCategoriesIds.Contains(y.Id)))
.ToList();
this is because ctx.Chairs is a collection that is in database, you should retrieve that collection first in order to compare it with in-memory data:
var result = ctx
.Chairs
.AsEnumerable() // retrieve data
.Where(x =>
x.Table.Categories.Any(y =>
myCategories.Any(z => z.Id == y.Id)))
.ToList();
EDIT: that wouldn't be the correct thing to do if you have a lot of entities on database, what you can do is to split it into two queries:
var tables = ctx.Tables
.Where(x =>
x.Categories.Any(y =>
myCategories.Any(z => z.Id == y.Id)));
var result = ctx.Chairs
.Where(x =>
tables.Any(t=> t.Id == x.TableId))
.ToList();
You can select Ids from myCategories and use it last statement.
var CategoryIds = myCategories.Select(ct => ct.Id);
var result = ctx.Chairs.Where(x => x.Table.Categories.Any(y => CategoryIds.Any(z => z == y.Id))).ToList();
I have a relationship, project categories. This relation is many to many so i have three tables: Project / project_has_category / categories.
I need to select all projects that has a relation with a certain category (by its id)
Project class
public class Project
{
public int ProjectID { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
Category class
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
I have tried the following:
[HttpPost]
public ActionResult Projects(string catID, string strSearch)
{
var cats = Adapter.CategoryRepository.Get();
var projects = Adapter.ProjectRepository.Get().Where(x => x.Categories.Contains(catID));
/*also*/
var projects = Adapter.ProjectRepository.Get().Where(x => cats.Contains(catID));
return View(projects);
}
But this gives the error:
The best overloaded method match for
'System.Collections.Generic.ICollection.Contains(LibModels.Category)'
has some invalid
arguments C:\Users\thomas\Desktop\Freelauncher1005\Freelauncher\Controllers\ProjectController.cs
What am I doing wrong?
Categories is a list of Category objects, you can't search for integer id with Contains method (check signature of this method - it requires Category object to search for):
var projects = Adapter.ProjectRepository.Get()
.Where(x => x.Categories.Contains(catID)) // error
Use Any to check if there is Category object with id equal to your value:
var projects = Adapter.ProjectRepository.Get()
.Where(x => x.Categories.Any(c => c.CategoryID == catID))
You need to cast the ID to int first and use Any then
var id = int.Parse(catID);
Adapter.ProjectRepository.Get().Where(x => x.Categories.Any(y => y.CategoryID == id))
var projects = Adapter.ProjectRepository
.Get()
.Where(p => p.Categories
.Any(c => c.CategoryID == catID));
or possibly
var projects = Adapter.CategoryRepository
.Get()
.Where(c => c.CategoryID == catID)
.Select(c => c.Projects)
.Distinct();
to query it from the other direction
I have object
public class OrderItem
{
public string idProduct { get; set; }
public int quantity { get; set; }
public List<WarehouseItem> WarehouseInfo = new List<WarehouseItem>();
}
public class WarehouseItem
{
public string Name{ get; set; }
public string LocnCode{ get; set; }
}
and i need select items which have WarehouseInfo.LocnCode == "A1"
It is doesnt work when I use something like
var items = itemList.Where(x => x.WarehouseInfo.Where(y => y.LocnCode.Equals("A1")));
Your requirements could be interpreted one of three ways, so here's three solutions:
Give me all OrderItems where ANY WarehouseItem has a LocnCode of "A1":
var items = itemList.Where(i => i.WarehouseInfo.Any(w => w.LocnCode == "A1"));
Give me all WarehouseItems within the OrderItems that have a LocnCode of "A1":
var items = itemList.SelectMany(i => i.WarehouseInfo)
.Where(w => w.LocnCode.Equals("A1"));
Give me all OrderItems where ANY WarehouseItem has a LocnCode of "A1", and filter WarehouseInfo to only those WarehouseItems:
This can't be done in a simple Linq query because there's no way to change the contents of the existing objects. You're going to have to create new objects with the filtered values:
var items = itemList.Where(i => i.WarehouseInfo.Any(w => w.LocnCode == "A1"))
.Select(i => new OrderItem
{
idProduct = i.idProduct,
quantity = i.quantity,
WarehouseInfo = i.WarehouseInfo.Where(w => w.LocnCode.Equals("A1"));
.ToList()
}
);
Try
var items = itemList.Where(x => x.WarehouseInfo.Any(y => y.LocnCode.Equals("A1")));
The Where takes a predicate that should return a bool. Any will return true if at least one item in the collection returns true for the given predicate.
Assume you have the following simple objects:
class Order
{
public Customer[] Customers { get; set; }
}
class Customer
{
public SaleLine[] SaleLines { get; set; }
}
class SaleLine
{
public Tax[] MerchTax { get; set; }
public Tax[] ShipTax { get; set; }
}
class Tax
{
public decimal Rate { get; set; }
public decimal Total { get; set; }
}
With these objects, I want to be able to get a list of all the unique tax rates used on the entire order, including both merchandise and shipping tax rates.
The following LINQ query will get me the list I need, but only for merchandise taxes:
var TaxRates = MyOrder.Customers
.SelectMany(customer => customer.SaleLines)
.SelectMany(saleline => saleline.MerchTax)
.GroupBy(tax => tax.Rate)
.Select(tax => tax.First().Rate
How can I get a list that contains list of unique tax rates that contains both merchandise and shipping rates?
It sounds like you want this:
var TaxRates = MyOrder.Customers
.SelectMany(customer => customer.SaleLines)
.SelectMany(saleline => saleline.MerchTax.Concat(saleline.ShipTax))
.GroupBy(tax => tax.Rate)
.Select(group => group.Key);
Basically the change is the call to Concat which will concatenate two sequences together.
It can also be used like this;
var TaxRates = MyOrder.Customers
.ToList()
.SelectMany(x => new[] { x.SaleLines, x.MerchTax })
.GroupBy(tax => tax.Rate)
.Select(group => group.Key);