I have a scenario in following nested
--Orders (List)
----Products (List)
------Manufacturers (List)
FIELDS
-Name
-Address
-City
In this scenario, I would need to execute query which will filter on City of Manufacturers and returns Orders, Products & only matching city manufacturers
I tried to put following query, however I am getting all list of Products even though city doesn't match to Manufacturers.
var filteredOrders = from o in Orders
from t in o.Products
where t.Manufacturers.Any(v => v.City == "Hartford")
select o;
Or even if I change from select o to 'select t.Manufacturers' I am getting all list of Manufacturers irrespective of city filter.
Luckily I got W3school SQL sample which matches to my scenario.
https://www.w3schools.com/sql/trysql.asp?filename=trysql_op_or
SQL Query:
SELECT o.OrderId, p.ProductName, s.*
FROM [Orders] o
JOIN OrderDetails od ON o.OrderId = od.OrderId AND o.orderId = 10248
JOIN Products p ON od.ProductId = p.ProductId
JOIN Suppliers s ON p.SupplierId = s.SupplierId and s.City ='Singapore'
I would flatten everything and then only filter on cities you want:
class Manufacturer
{
public string Name;
public string Address;
public string City;
}
class Product
{
public Manufacturer[] Manufacturers;
}
class Order
{
public Product[] Products;
}
static void Main(string[] args)
{
var cities = new string[] { "a", "b" };
Order[] orders = null;
orders.SelectMany(o => o.Products.SelectMany(p => p.Manufacturers.Select(m => new { o, p, m })))
.Where(g => cities.Contains(g.m.City))
.ToList();
}
Alternatively, if you want to return new Orders (because they have a different Products, it MUST point to a newly allocated Object) you could have this instead:
var newOrders = orders.Select(o => new Order()
{
Products = o.Products
.Select(p => new Product()
{
Manufacturers = p.Manufacturers.Where(m => cities.Contains(m.City)).ToArray()
})
.Where(m => m.Manufacturers.Length > 0).ToArray()
}).Where(p => p.Products.Length > 0).ToArray();
I finally tried to put everything together and got the expected output.
var fp = orders.Select(o =>
{
o.products = o.products.Select(p =>
{
p.manufacturers.RemoveAll(m => m.City != "Hartford");
return p;
}).ToList();
return o;
});
Please suggest if anyone has better solution
You are applying your City filter wrong. It is this line.
where t.Manufacturers.Any(v => v.City == "Hartford")
Any return true, at least one of the manufacturers has City property as "Hartford" so basically your query is something like this
var filteredOrders = from o in Orders
from t in o.Products
where true//←This is the problem
select o;
What you need to do is in fact
where t.Manufacturers.City == "Hartford"
I hope this helps
Example:
var cityNames = new List<string> {"New York",
"Atlanta",
"Hartford",
"Chicago"
};
var anyResult = cityNames.Any(x=>x== "Hartford"); //TRUE
var whereResult = cityNames.Where(x => x == "Hartford"); //IEnumerable<string>, in this case only one element
I cannot think of a way which can completely avoid creating new objects, as the parent object's list property cannot be filtered directly. You can make use of the same class though.
Also I use two separate queries in order to create a new list in parent / grandparent object.
I have made a small demo to demonstrate the idea (below has equivalent code):
http://ideone.com/MO6M6t
The city I try to select is "tmp" which only under parent p3, which only belongs to grand parent g1, g3
The expected output is:
g1
p3
tmp
g3
p3
tmp
using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public class GrandParent{
public List<Parent> parentList{ get; set; }
public string name{ get; set; }
public GrandParent(string name){
this.name = name;
this.parentList = new List<Parent>();
}
}
public class Parent{
public List<Child> childList{ get; set;}
public string name{ get; set; }
public Parent(string name){
this.name = name;
this.childList = new List<Child>();
}
}
public class Child{
public string city{ get; set;}
public Child(string city){
this.city = city;
}
}
public static void Main()
{
Child c1 = new Child("ABC"), c2 = new Child("123"), c3 = new Child("tmp");
Parent p1 = new Parent("p1"), p2 = new Parent("p2"), p3 = new Parent("p3");
GrandParent g1 = new GrandParent("g1"), g2 = new GrandParent("g2"), g3 = new GrandParent("g3");
p1.childList.Add(c1); p1.childList.Add(c2);
p2.childList.Add(c2);
p3.childList.Add(c3);
g1.parentList.Add(p1); g1.parentList.Add(p2); g1.parentList.Add(p3);
g2.parentList.Add(p2);
g3.parentList.Add(p3);
List<GrandParent> repo = new List<GrandParent>{g1, g2, g3};
var filteredParents = from g in repo
from p in g.parentList
where p.childList.Any(c => c.city == "tmp")
select new Parent(p.name){
childList = p.childList.Where(c => c.city == "tmp").ToList()
};
var filteredGrandParents = from g in repo
from p in g.parentList
where filteredParents.Any(fp => fp.name == p.name)
select new GrandParent(g.name){
parentList = g.parentList.Where(pp => filteredParents.Any(fp => fp.name == pp.name)).ToList()
};
foreach(var g in filteredGrandParents){
Console.WriteLine(g.name);
foreach(var p in g.parentList){
Console.WriteLine("\t" + p.name);
foreach(var c in p.childList){
Console.WriteLine("\t\t" + c.city);
}
}
Console.WriteLine();
}
}
}
Related
I checked the questions that may already have my answer but it seems that none of them addresses the issue I have:
var semesters = db.Semesters.Where(e => e.ID == semester).ToList();
var semestersbyfaculty = db.SemestersByFaculty.Where(e => e.FacultyID == id).ToList();
How do I inner join these two queries like if I do the following in SQL:
SELECT
Fac.*, Sem.SemesterText
FROM
SemestersByFaculty AS Fac
INNER JOIN
Semesters AS Sem
ON
Fac.SemesterID = Sem.ID
WHERE id
Fac.FacultyID = id
Inner Join
The following example shows a simple inner equijoin.
var query = from fac in db.SemesterByFaculty
join sem in db.Semester on fac.SemesterID equals sem.ID
where fac.FacultyID == id
select new { Faculty = fac, SemesterText = sem.SemesterText };
For more information, see How to: Perform Inner Joins (C# Programming Guide).
UPDATE:
from comments
Models
class MyModel {
public MitModel.SemestersByFaculty Faculty{ get; set; }
public string SemesterText { get; set; }
}
class MyViewModel {
public List<MyModel> SemesterFaculties { get; set; }
}
Action:
public ActionResult SomeAction(string id) {
var query = from fac in db.SemesterByFaculty
join sem in db.Semester on fac.SemesterID equals sem.ID
where fac.FacultyID == id
select new MyModel{ Faculty = fac, SemesterTest = sem.SemesterTest };
var viewModel = new MyViewModel { SemesterFaculties = query.ToList() };
return View(viewModel);
}
View
#Model MyViewModel
Suppose you have following fields in SemestersByFaculty
class SemestersByFaculty
{
string FacultyName;
int FacultyID;
int SemesterID;
}
Note : If you have more fields in SemestersByFaculty class then you can list them in new{} in query below:
var query = semesters.Join(semestersbyfaculty,
sem => sem.ID,
fac => fac.SemesterID,
(sem, fac) =>
new { facName = fac.FacultyName, facId = fac.FacultyID,semText = sem.SemesterText }).Where(e=> e.fac.FacultyId = id);
Now , what you have got yourself is an enumerable. You can iterate over it and retrieve the values. It will be like:
foreach (var obj in query){
Console.writeln("{0}-{1}-{2}",obj.facName,obj.facId,obj.semText);
}
Here is solution with non-LINQ syntax. Also you can perform 'Where' filter on 'db.Semesters' and 'db.SemestersByFaculty' before they are passed into 'join'.
var result = db.SemestersByFaculty
.Where(
x_ => x_.FacultyID == id)
.Join(
db.Semesters.Where(x_ => x_.ID == semester),
x_ => x_.SemesterID,
x_ => x_.ID,
(x_, y_) => new
{
FacultyID = x_.ID,
SemesterID = y_.SemesterID,
Sem = y_.SemesterText
})
.ToList();
I am trying to get a list filtered based on the matches of one of the properties with a property of another list.
In below example, only the items which have common 'name' between both lists should be filtered in 1st list. Can some one tell me the most concise way of doing it?
class TCapability
{
public string Name { get; set; }
public int Id { get; set; }
}
class PCapability
{
public string Name { get; set; }
public int Code { get; set; }
}
Input:
var capability = new List<TCapability>()
{
new TCapability() {Name="a", Id=1},
new TCapability() {Name="b", Id=2},
new TCapability() {Name="c", Id=3}
};
var type2Capability = new List<PCapability>()
{
new PCapability() {Name="a", Code=100},
new PCapability() {Name="b", Code=200},
new PCapability() {Name="d", Code=300}
};
Expected Output:
capability =
{
{ Name="a", Id=1 },
{ Name="b", Id=2 }
}
var result = capability.Where(c => type2Capability.Any(c2 => c.Name == c2.Name));
you can try use join clause like this
capability = (from a in capability
join b in type2Capability on a.Name equals b.Name
select a).ToList();
UPDATE on comment if type2Capability can have duplicate names
capability = (from a in capability
join b in type2Capability on a.Name equals b.Name into f
where f.Any()
select a).ToList();
If the lists can get long then a HashSet can speed things up.
var set = new HashSet<string>(type2Capability.Select(t => t.Name));
var res = capability.Where(c => set.Contains(c.Name));
I have a certain calculated field that I regularly want to return in the "select" fields of a Linq query, e.g. Customer order total this year, along with other demographic info of the Customer.
public class Customer {
public decimal TotalPurchasesThisYear(MyDataContext db) {
return db.Orders.Where(o => o.CustomerID == ID)
.Sum(o => o.OrderTotalAmt);
}
}
public class SomeReport {
public void GetCustomerInfoBySalesperson(long salespersonID) {
using (var db = new MyDataContext()) {
var q = db.Customers.Where(c => c.SalespersonID == salespersonID)
.Select(c => new { c.Name, c.Address, ThisYearPurchases = c.TotalPurchasesThisYear(db) })
.ToList();
// etc..
}
}
}
Obviously, this doesn't work, because TotalPurchasesThisYear has no SQL translation. But everything inside it does have a SQL translation. I don't want to include that code directly in the query, because I'm doing the same calculation in lots of places. My gut tells me this should be done with an Expression but I've played around and can't quite work out the right syntax.
Help, anyone?
Thanks!
OK, I've found one way to do this, though I'm not sure it's the best way:
public class CustomerOrderInfo {
public long CustomerID;
public decimal TotalPurchases;
}
public class Customer {
public static Expression<Func<Customer, CustomerOrderInfo>> GetCustomerOrderInfo() {
return c => new CustomerOrderInfo {
CustomerID = c.ID,
TotalPurchases = c.Orders.Where(o => o.OrderDate.Year == DateTime.Now.Year)
.Sum(o => o.OrderTotalAmt)
};
}
}
public class SomeReport {
public void GetCustomerInfoBySalesperson(long salespersonID) {
using (var db = new MyDataContext()) {
var q = db.Customers
.Join(db.Customers.Select(Customer.GetCustomerOrderInfo()),
c => c.ID, i => i.CustomerID,
(c, i) => new { c.Name, c.Address, i.TotalPurchases })
.ToList();
// etc..
}
}
}
It works... but the underlying SQL ends up with a join on a nested query, which isn't my ideal. I would want to see the underlying SQL be something simple, like:
select c.Name, c.Address, sum(o.OrderTotalAmt) TotalPurchases
from Customer c
left join [Order] o on o.CustomerID = c.ID
where Year(o.OrderDate) = #year
group by c.Name, c.Address
List<Parent> myParents = new List<Parent>
{
new Parent()
{
Prop1 = "1",
Prop2 = "2",
Prop3 = "3",
Children = new List<Child>()
{
new Child(){ Prop1 = 1, Prop2 = 2, Prop3 = 3 },
new Child(){ Prop1 = 21, Prop2 = 22, Prop3 = 23 },
new Child(){ Prop1 = 31, Prop2 = 32, Prop3 = 33 }
}
},
};
Expression<Func<Parent, int>> GetChildSum =
p => p.Children.Where(c => c.Prop1 > 0).Sum(o => o.Prop2);
var v = myParents.Where(w => w.Prop1 == "1").Select(p => GetChildSum.Compile().Invoke(p)).ToList();
Console.WriteLine(v.First());
//output is 56
I keep getting the error below on my code, and can't understand why it is having problems translating it to a query, it is pretty simple.
I have 2 repositories, Album and AlbumImage, when I fetch an album do I want a cover, that is a subselect in AlbumImages. What am I doing wrong here?
LINQ to Entities does not recognize the method
'System.Linq.IQueryable`1[Sogaard.us.Cosplay.Data.AlbumImage] Get()'
method, and this method cannot be translated into a store expression.
Album repository
public class AlbumRepository : IRepository<Album>
{
private CosplayEntities _entities;
private IRepository<AlbumImage> _imageRepository;
public AlbumRepository(CosplayEntities entities, IRepository<AlbumImage> imageRepository)
{
_entities = entities;
_imageRepository = imageRepository;
}
public IQueryable<Album> Get()
{
return (from a in _entities.Albums
select new Album()
{
Id = a.Id,
UserId = a.UserId,
Name = a.Name,
Created = a.Created,
LastEdit = a.LastEdit,
Description = a.Description,
Views = a.Views,
Location = a.Location,
Photoshoot = a.Photoshoot,
Cover = (from ai in _imageRepository.Get() where ai.AlbumId == a.Id orderby ai.Cover descending, ai.Id ascending select ai).FirstOrDefault(),
});
}
}
AlbumImage repository
public class AlbumImageRepository : IRepository<AlbumImage>
{
private CosplayEntities _entities;
public AlbumImageRepository(CosplayEntities entities)
{
_entities = entities;
}
public IQueryable<AlbumImage> Get()
{
return (from ai in _entities.AlbumImages
select new AlbumImage()
{
Id = ai.Id,
AlbumId = ai.AlbumId,
UserId = ai.UserId,
Type = ai.Type,
Width = ai.Width,
Height = ai.Height,
Description = ai.Description,
Views = ai.Views,
Uploadet = ai.Uploadet,
LastView = ai.LastView,
Thumblink = ai.Thumblink,
Imagelink = ai.Imagelink,
Cover = ai.Cover
});
}
This is the code i am getting the error on
_albumImageRepository = new AlbumImageRepository(_entities);
_albumRepository = new AlbumRepository(_entities, _albumImageRepository);
_albumImagesTagRepository = new AlbumImagesTagRepository(_entities);
....
var album = _albumRepository.Get().Where(x => x.Id == image.AlbumId).FirstOrDefault();
Update: I have commented the Cover = ... out in my IQueryable Get() so it is 2 simple select as object.
And i still get the error in something as simple as
model.Albums = (from a in _albumRepository.Get()
orderby a.Id descending
select new AlbumDisplayModel()
{
Album = a,
ImageCount = _albumImageRepository.Get().Where(x => x.AlbumId == a.Id).Count(),
User = _userRepository.Get().Where(x => x.Id == a.UserId).FirstOrDefault()
})
.Skip(AlbumsPrPage * (page - 1))
.Take(AlbumsPrPage).ToList();
Update 2: If i rewrite the IQueryable Get() to the following, do it work flawlessly, there there should really be no diffrence in how it is handled?
public IQueryable<Album> Get()
{
return (from a in _entities.Albums
select new Album()
{
Id = a.Id,
UserId = a.UserId,
Name = a.Name,
Created = a.Created,
LastEdit = a.LastEdit,
Description = a.Description,
Views = a.Views,
Location = a.Location,
Photoshoot = a.Photoshoot,
Cover = (from ai in _entities.AlbumImages where ai.AlbumId == a.Id orderby ai.Cover descending, ai.Id ascending select new AlbumImage()
{
Id = ai.Id,
AlbumId = ai.AlbumId,
UserId = ai.UserId,
Type = ai.Type,
Width = ai.Width,
Height = ai.Height,
Description = ai.Description,
Views = ai.Views,
Uploadet = ai.Uploadet,
LastView = ai.LastView,
Thumblink = ai.Thumblink,
Imagelink = ai.Imagelink,
Cover = ai.Cover
}).FirstOrDefault(),
});
}
Update 3: Did a little test, and the problem seems to be with Entity framework, se the following code, The var linqAlbum = testClass.LinqAlbumGet().ToList(); executes without any problems and return the correct data, var eeAlbum = testClass.EEAlbumGet().ToList(); fails with the exception
LINQ to Entities does not recognize the method
'System.Linq.IQueryable`1[RepositoryTest.TestAlbumCover] EEImageGet()'
method, and this method cannot be translated into a store expression.
My test script
class Program
{
static void Main(string[] args)
{
var linq = new LinqDataContext();
var ee = new NewCosplayEntities();
var testClass = new Test(linq, ee);
var linqAlbum = testClass.LinqAlbumGet().ToList();
var eeAlbum = testClass.EEAlbumGet().ToList();
}
}
public class Test
{
public NewCosplayEntities ee { get; set; }
public LinqDataContext linq { get; set; }
public Test(LinqDataContext linq, NewCosplayEntities ee)
{
this.linq = linq;
this.ee = ee;
}
public IQueryable<TestAlbum> LinqAlbumGet()
{
return from a in linq.Albums
select new TestAlbum
{
Id = a.Id,
Name = a.Name,
Cover = (from i in LinqImageGet() where i.AlbumId == a.Id select i).FirstOrDefault()
};
}
public IQueryable<TestAlbumCover> LinqImageGet()
{
return from i in linq.AlbumImages
select new TestAlbumCover()
{
Id = i.Id,
AlbumId = i.AlbumId
};
}
public IQueryable<TestAlbum> EEAlbumGet()
{
return from a in ee.Albums
select new TestAlbum
{
Id = a.Id,
Name = a.Name,
Cover = (from i in EEImageGet() where i.AlbumId == a.Id select i).FirstOrDefault()
};
}
public IQueryable<TestAlbumCover> EEImageGet()
{
return from i in ee.AlbumImages
select new TestAlbumCover()
{
Id = i.Id,
AlbumId = i.AlbumId
};
}
}
public class TestAlbum
{
public int Id { get; set; }
public string Name { get; set; }
public TestAlbumCover Cover { get; set; }
}
public class TestAlbumCover
{
public int Id { get; set; }
public int AlbumId { get; set; }
}
Your problem comes in the ItemRepository for Albumn. Specifically because _entities has no knowledge of the _imageRepository type, so it doesn't know how to translate that type into the appropriate TSQL script. You could cast the _entities.Albums.ToList() to force the IQueryable into an IEnumerable before you try to access the _ImageRepository.Get() from the scope of the hydrated object instead of directly on the database instance. Realize that you are then going to see a perf hit on the n+1 database requests for the AlbumImage child objects for each Album.
public IQueryable<Album> Get()
{
return (from a in _entities.Albums
select new Album()
{
Id = a.Id,
UserId = a.UserId,
Name = a.Name,
Created = a.Created,
LastEdit = a.LastEdit,
Description = a.Description,
Views = a.Views,
Location = a.Location,
Photoshoot = a.Photoshoot,
Cover = (from ai in _imageRepository.Get() where ai.AlbumId == a.Id orderby ai.Cover descending, ai.Id ascending select ai).FirstOrDefault(),
});
}
Ultimately, the problem is that your trying to use an ActiveRecord pattern rather than a true repository. Everything in a single IQueryable needs to be fetched through the same database context instance for parsing and tracking purposes.
Potentially its because you are wrapping the Album and AlbumImage in new references. I would remove that and do the projection after your query.
I don't think you can project into an entity and have each projection use a result from another IQueryable. If you replaced the contents of IQueryable<AlbumImage> Get() with this, it might work:
from a in _entities.Albums
join c in _imageRepository.Get() on a.Id equals c.AlbumId into acJoin
from ac in acJoin.DefaultIfEmpty()
select new Album()
{
Id = a.Id,
etc..,
etc..,
Cover = ac
}
I'm actually fairly certain that you will need to adjust this freehand query, but essentially it's joining the IQueryables, then projecting those results into your objects, instead of projecting to your objects then inserting an IQueryable into those results. Not the best explanation I know, but just look up "LINQ Left Join" or "Linq Left Outer Join" to see the syntax of what I'm describing here. Example
Assuming
public class MyClass
{
public int ID {get; set; }
public string Name {get; set; }
}
and
List<MyClass> classList = //populate with MyClass instances of various IDs
I can do
List<MyClass> result = classList.FindAll(class => class.ID == 123);
and that will give me a list of just classes with ID = 123. Works great, looks elegant.
Now, if I had
List<List<MyClass>> listOfClassLists = //populate with Lists of MyClass instances
How do I get a filtered list where the lists themselves are filtered. I tried
List<List<MyClass>> result = listOfClassLists.FindAll
(list => list.FindAll(class => class.ID == 123).Count > 0);
it looks elegant, but doesn't work. It only includes Lists of classes where at least one class has an ID of 123, but it includes ALL MyClass instances in that list, not just the ones that match.
I ended up having to do
List<List<MyClass>> result = Results(listOfClassLists, 123);
private List<List<MyClass>> Results(List<List<MyClass>> myListOfLists, int id)
{
List<List<MyClass>> results = new List<List<MyClass>>();
foreach (List<MyClass> myClassList in myListOfLists)
{
List<MyClass> subList = myClassList.FindAll(myClass => myClass.ID == id);
if (subList.Count > 0)
results.Add(subList);
}
return results;
}
which gets the job done, but isn't that elegant. Just looking for better ways to do a FindAll on a List of Lists.
Ken
listOfClasses.SelectMany(x=>x).FindAll( /* yadda */)
Sorry about that, FindAll is a method of List<T>.
This
var result = from x in listOfClasses from y in x where SomeCondition(y) select y;
or
var result = listOfClasses.SelectMany(x=>x).Where(x=>SomeCondition(x));
To keep a list of lists, you could do something like this example:
MyClass a = new MyClass() { ID = 123, Name = "Apple" };
MyClass b = new MyClass() { ID = 456, Name = "Banana" };
MyClass c = new MyClass() { ID = 789, Name = "Cherry" };
MyClass d = new MyClass() { ID = 123, Name = "Alpha" };
MyClass e = new MyClass() { ID = 456, Name = "Bravo" };
List<List<MyClass>> lists = new List<List<MyClass>>()
{
new List<MyClass>() { a, b, c },
new List<MyClass>() { d, e },
new List<MyClass>() { b, c, e}
};
var query = lists
.Select(list => list.Where(item => item.ID == 123).ToList())
.Where(list => list.Count > 0).ToList();
query would be List<List<MyClass>> holding lists of MyClass objects that passed the test. At first glance, it looks out of order with the Where extension coming after the Select, but the transformation of the inner lists needs to occur first, and that's what's happening in the Select extension. Then it is filtered by the Where.
I would probably go with this
List<List<string>> stuff = new List<List<string>>();
List<List<string>> results = new List<List<string>>();
stuff.ForEach(list=> {var result = list.FindAll(i => i == "fun").ToList();
if (result.Count > 0) results.Add(result);
});
List<string> flatResult = new List<string>();
stuff.ForEach(List => flatResult.AddRange(List.FindAll(i => i == "fun")));
That way you can go with a jagged array or flatten it out.. But the Linq way works well too :-).
While producing a flat List<MyClass> will answer your need most of the time, the exact answer to your question is:
var result = (from list in ListOfClassLists
let listWithTheId=
(
(from myClass in list
where myClass.ID == id
select myClass)
.ToList()
)
where listWithTheId.Count > 0
select listWithTheId
).ToList();
This code snippet was taken from my Proof of Concept:
using System.Collections.Generic;
using System.Linq;
namespace ListOfListSelectionSpike
{
public class ListSpikeClass
{
public List<List<MyClass>> ListOfClassLists { get; set; }
private List<MyClass> list1, list2, list3;
public ListSpikeClass()
{
var myClassWithId123 = new MyClass("123");
var myClassWithIs345 = new MyClass("456");
list1 = new List<MyClass> { myClassWithId123, myClassWithIs345 };
list2 = new List<MyClass> { myClassWithId123, myClassWithIs345, myClassWithId123 };
list3 = new List<MyClass> { myClassWithIs345, myClassWithIs345 };
ListOfClassLists = new List<List<MyClass>> { list1, list2, list3 };
}
public List<List<MyClass>> GetListOfListsById(string id)
{
var result = (from list in ListOfClassLists
let listWithTheId =
((from myClass in list
where myClass.ID == id
select myClass)
.ToList())
where listWithTheId.Count > 0
select listWithTheId)
.ToList();
return result;
}
}
public class MyClass
{
public MyClass(string id)
{
ID = id;
Name = "My ID=" + id;
}
public string ID { get; set; }
public string Name { get; set; }
}
}