I'm trying to left join multiple tables and project some columns that result from this join onto a new entity, and then taking a few records from this result from my database. I've taken a look on a few similar questions here on SF, but I'm not managing to assemble all of those parts into a piece of code that works.
Here is the query I'm trying to generate with NHibernate:
select * from
( select LOC_Key.KeyName, LOC_Translation.TranslationString, LOC_Translation.Comments
from LOC_Key
left join LOC_Translation
on LOC_Key.ID = LOC_Translation.KeyID and LOC_Translation.LanguageID = 6
order by LOC_Key.KeyName
) as keyTable
limit 0,100
I have three entities here, Key, Translation and Language. A Key is a unique string identifier for different translations of a same word in different languages. I want to show the first n keys in alphabetical order for a language, but I want all keys listed, not only the ones that are translated (that's why I'm using a left join).
I took a look at QueryOver<>, Select() method and List<object[]>() method but I can't even manage to have a code that compiles in the first place.
I could use C# linq after getting all records from the tables Key and Translation, having something like this:
IEnumerable<string> k = RepositoryKey.GetLimits( offset, size ).Select( x => x.KeyName );
IEnumerable<TranslationDescriptor> t = RepositoryTranslation.GetAllWhere( x => x.LanguageID.LanguageCode == language && k.Contains ( x.KeyID.KeyName ) ).ToList().ConvertAll( new Converter<Translation, TranslationDescriptor>( ( x ) => { return new TranslationDescriptor { LanguageCode = x.LanguageID.LanguageCode, KeyName = x.KeyID.KeyName, Comments = x.Comments, TranslationString = x.TranslationString }; } ) );
var q = from key in k
join trl in t on key equals trl.KeyName into temp
from tr in temp.DefaultIfEmpty()
select new TranslationDescriptor { KeyName = key, LanguageCode = language, Comments = ( tr == null ) ? string.Empty : tr.Comments, TranslationString = ( tr == null ) ? string.Empty : tr.TranslationString };
However, that's very slow. By the way, my implementation for GetLimits and GetAllWhere is:
public IEnumerable<T> GetAllWhere(Func<T, bool> func)
{
var products = Session.Query<T>().Where(func);
return products;
}
public IEnumerable<T> GetLimits(int offset, int size)
{
return Session.CreateCriteria(typeof(T)).SetFirstResult(offset).SetMaxResults(size).List<T>();
}
Thank you for your help!
Bruno
I'm guessing a little bit at your entities and mappings, but the following might help you get ideas. It joins Key to Translation with a left outer join, then projects the results to a new DTO object.
[Test]
public void LeftOuterProjection()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
// Set up aliases to use in the queryover.
KeyDTO dtoAlias = null;
Key keyAlias = null;
Translation translationAlias = null;
var results = s.QueryOver<Key>(() => keyAlias)
.JoinAlias(k => k.Translations, () => translationAlias, JoinType.LeftOuterJoin)
.Where(() => translationAlias.LanguageId == 6)
.OrderBy(() => keyAlias.KeyName).Asc
.Select(Projections.Property(() => keyAlias.KeyName).WithAlias(() => dtoAlias.KeyName),
Projections.Property(() => translationAlias.TranslationString).WithAlias(() => dtoAlias.TranslationString),
Projections.Property(() => translationAlias.Comments).WithAlias(() => dtoAlias.Comments))
.TransformUsing(Transformers.AliasToBean<KeyDTO>())
.List<KeyDTO>();
}
}
public class KeyDTO
{
public string KeyName { get; set; }
public string TranslationString { get; set; }
public string Comments { get; set; }
}
public class Key
{
public int Id { get; set; }
public string KeyName { get; set; }
public IList<Translation> Translations { get; set; }
}
public class Translation
{
public Key Key { get; set; }
public int LanguageId { get; set; }
public string TranslationString { get; set; }
public string Comments { get; set; }
}
The modifications I made to my code following ngm suggestion (thanks!):
Language l = RepositoryLanguage.GetSingleOrDefault(x => x.LanguageCode
== language);
KeyTranslationDTO dtoAlias = null;
Key keyAlias = null;
Translation translationAlias = null;
var results = RepositoryKey.GetSession()
.QueryOver<Key>(() => keyAlias)
.OrderBy(() => keyAlias.KeyName).Asc
.JoinQueryOver<Translation>( k => k.Translations, () => translationAlias, JoinType.LeftOuterJoin, Restrictions.Where( () => translationAlias.LanguageID == l ) )
.Select(Projections.Property(() => keyAlias.KeyName).WithAlias(() => dtoAlias.KeyName),
Projections.Property(() => translationAlias.TranslationString).WithAlias(() => dtoAlias.TranslationString),
Projections.Property(() => translationAlias.Comments).WithAlias(() => dtoAlias.Comments))
.TransformUsing(Transformers.AliasToBean<KeyTranslationDTO>())
.Skip(index).Take(amount)
.List<KeyTranslationDTO>();
Related
So, I have my Products table in SSMS with these properties:
public class Product
{
public int Id {get; set;}
public string Title { get; set; }
public decimal Price { get; set; }
}
and my Reports table:
public class Report
{
public int Id { get; set; }
public int ProductId { get; set; }
public ReportType ReportType { get; set; }
}
I want to return a List<Product> to my View that is sorted based on how many reports each Product has, but I can't figure out how to do it with LINQ. Any help/tip would be appreciated.
If you put nav props in this would be:
context.Products.Include(p => p.Reports).OrderBy(p => p.Reports.Count(*));
But as you have no nav props, perhaps something like:
context.Products.OrderBy(p => context.Reports.Count(r => r.ProductId == p.Id));
The query ends up looking like this for the latter:
SELECT *
FROM p
ORDER BY (SELECT COUNT(*) FROM r WHERE p.id = r.id)
and similar but with a left join, for the former
You could also do it on the client side
var dict = context.Reports.GroupBy(r => ProductId, (k,g) => new { ProductId, Count = g.Count() } )
.ToDictionary(at => at.ProductId, at => at.Count);
Then:
//or OrderByDescending if you want most reported products
var ret = context.Products.ToList().OrderBy(p => dict[p.ProductId]);
If you have some limited list of products:
var prods = context.Products.Where(...).ToList();
var prodIds = prods.Select(p => p.ProductId).ToArray();
var dict = context.Reports
.Where(r => prods.Contains(r.ProductId))
.GroupBy(r => ProductId, (k,g) => new { ProductId, Count = g.Count() } )
.ToDictionary(at => at.ProductId, at => at.Count)
var ret = prods.OrderBy(p => dict[p.ProductId]);
I have to next 2 entities in my project
public class Product
{
public Product()
{
this.ProductImages = new HashSet<ProductImage>();
this.ProductParams = new HashSet<ProductParam>();
}
public int ID { get; set; }
public int BrandID { get; set; }
public int CodeProductTypeID { get; set; }
public string SeriaNumber { get; set; }
public string ModelNumber { get; set; }
public decimal Price { get; set; }
public bool AvailableInStock { get; set; }
public virtual Brand Brand { get; set; }
public virtual CodeProductType CodeProductType { get; set; }
public virtual ICollection<ProductImage> ProductImages { get; set; }
public virtual ICollection<ProductParam> ProductParams { get; set; }
}
public class ProductParam
{
public int Id { get; set; }
public int ProductId { get; set; }
public int CodeProductParamId { get; set; }
public string Value { get; set; }
public virtual Product Product { get; set; }
public virtual CodeProductParam CodeProductParam { get; set; }
}
and I want to get list of Products which has list of specified parameters
var prodParamCritria = new List<ProductParam>()
{
new ProductParam(){CodeProductParamId =1, Value="Black" },
new ProductParam(){CodeProductParamId =2, Value="Steal"}
};
in sql I can do it by using EXISTS clause twise
SELECT *
FROM Products p
WHERE EXISTS (
SELECT *
FROM ProductParams pp
WHERE pp.ProductId = p.ID
AND (pp.CodeProductParamId = 1 AND pp.[Value] = N'Black')
)
AND EXISTS (
SELECT *
FROM ProductParams pp
WHERE pp.ProductId = p.ID
AND pp.CodeProductParamId = 2
AND pp.[Value] = N'Steal'
)
How can i get same result by EF methods or linq
Try this:
var products= db.Products.Where(p=>p.ProductParams.Any(pp=>pp.CodeProductParamId == 1 && pp.Value == "Black") &&
p.ProductParams.Any(pp=>pp.CodeProductParamId == 2 && pp.Value == "Steal"));
Update
The problem in work with that list of ProductParam to use it as a filter is that EF doesn't know how to translate a PodructParam object to SQL, that's way if you execute a query like this:
var products2 = db.Products.Where(p => prodParamCritria.All(pp => p.ProductParams.Any(e => pp.CodeProductParamId == e.CodeProductParamId && pp.Value == e.Value)));
You will get an NotSupportedException as you comment in the answer of #BostjanKodre.
I have a solution for you but probably you will not like it. To resolve that issue you could call the ToList method before call the Where. This way you will bring all products to memory and you would work with Linq to Object instead Linq to Entities, but this is extremely inefficient because you are filtering in memory and not in DB.
var products3 = db.Products.ToList().Where(p => prodParamCritria.All(pp => p.ProductParams.Any(e => pp.CodeProductParamId == e.CodeProductParamId && pp.Value == e.Value)));
If you want filter by one criteria then this could be more simple and you are going to be able filtering using a list of a particular primitive type. If you, for example, want to filter the products only by CodeProductParamId, then you could do this:
var ids = new List<int> {1, 2};
var products = db.Products.Where(p => ids.All(i=>p.ProductParams.Any(pp=>pp.CodeProductParamId==i))).ToList();
This is because you are working with a primitive type and not with a custom object.
I suppose something like that should work
db.Product.Where(x => x.ProductParams.FirstOrDefault(y => y.CodeProductParamId == 1) != null && x.ProductParams.FirstOrDefault(y => y.CodeProductParamId == 2) != null).ToList();
or better
db.Product.Where(x => x.ProductParams.Any(y => y.CodeProductParamId == 1) && x.ProductParams.Any(y => y.CodeProductParamId == 2)).ToList();
Ok, if you need to make query on parameters in list prodParamCriteria it will look like this:
db.Product.Where(x => prodParamCritria.All(c=> x.ProductParams.Any(p=>p.CodeProductParamId == c.CodeProductParamId && p.Value== c.Value))).ToList();
I forgot that complex types cannot be used in query database, so i propose you to convert your prodParamCriteria to dictionary and use it in query
Dictionary<int, string> dctParams = prodParamCritria.ToDictionary(x => x.CodeProductParamId , y=>y.Value);
db.Product.Where(x => dctParams.All(c => x.ProductParams.Any(p=> p.CodeProductParamId == c.Key && p.Value== c.Value))).ToList();
another variation:
IEnumerable<Int32> lis = prodParamCritria.Select(x => x.CodeProductParamId).ToList();
var q = Products.Select(
x => new {
p = x,
cs = x.ProductParams.Where(y => lis.Contains(y.Id))
}
).Where(y => y.cs.Count() == lis.Count()).
ToList();
with a named class like (or maybe without, but not in linqpad)
public class daoClass {
public Product p {get; set;}
public Int32 cs {get; set;}
}
IEnumerable<Int32> lis = prodParamCritria.Select(x => x.CodeProductParamId).ToList();
var q = Products.Select(
x => new daoClass {
p = x,
cs = x.ProductParams.Where(y => lis.Contains(y.Id))
}
).Where(y => y.cs.Count() == lis.Count()).
SelectMany(y => y.p).
ToList();
I have the following class
public class SolicitacaoConhecimentoTransporte
{
public long ID { get; set; }
public string CodigoOriginal { get; set; }
public DateTime Data { get; set; }
public List<CaixaConhecimentoTransporte> Caixas { get; set; }
}
I would like to know if there is a way of achiveing the same behavior of the code below using Linq (with lambda expression syntax),
List<SolicitacaoConhecimentoTransporte> auxList = new List<SolicitacaoConhecimentoTransporte>();
foreach (SolicitacaoConhecimentoTransporte s in listaSolicitacao)
{
SolicitacaoConhecimentoTransporte existing =
auxList.FirstOrDefault(f => f.CodigoOriginal == s.CodigoOriginal &&
f.Data == s.Data &&
f.ID == s.ID);
if (existing == null)
{
auxList.Add(s);
}
else
{
existing.Caixas.AddRange(s.Caixas);
}
}
return auxList;
In other words, group all entities that have equal properties and flat all lists into one.
Thanks in advance.
Use anonymous object to group by three properties. Then project each group to new SolicitacaoConhecimentoTransporte instance. Use Enumerable.SelectMany to get flattened sequence of CaixaConhecimentoTransporte from each group:
listaSolicitacao.GroupBy(s => new { s.CodigoOriginal, s.Data, s.ID })
.Select(g => new SolicitacaoConhecimentoTransporte {
ID = g.Key.ID,
Data = g.Key.Data,
CodigoOriginal = g.Key.CodigoOriginal,
Caixas = g.SelectMany(s => s.Caixas).ToList()
}).ToList()
I need to make a search based on a set of keywords, that return all the Ads related with those keywords. Then the result is a list of Categories with the Ads Count for each Category.
The search is made in a KeywordSearch Table:
public class KeywordSearch
{
public int Id { get; set; }
public string Name { get; set; }
public Keyword Keyword { get; set; }
}
Where the Keyword Table is:
public class Keyword
{
public int Id { get; set; }
public string Name { get; set; }
}
The Ads are related with the Keywords using the following Table:
public class KeywordAdCategory
{
[Key]
[Column("Keyword_Id", Order = 0)]
public int Keyword_Id { get; set; }
[Key]
[Column("Ad_Id", Order = 1)]
public int Ad_Id { get; set; }
[Key]
[Column("Category_Id", Order = 2)]
public int Category_Id { get; set; }
}
Finally, the Category table:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
Example:
Keywords: "Mercedes-Benz" and "GLK"
KeywordSearch: "Mercedes" and "Benz" for the Keyword "Mercedes-Benz"
"GLK" for the Keyword "GLK"
Category: "Cars" and "Trucks"
Ads: Car - Mercedes-Benz GLK
Truck - Mercedes-Benz Citan
If I search "Mercedes-Benz" I get:
Cars: 1
Trucks: 1
If I search "Mercedes-Benz GLK" I get:
Cars: 1
If I search "Mercedes Citan" I get:
Trucks: 1
What I get until now:
var keywordIds = from k in keywordSearchQuery
where splitKeywords.Contains(k.Name)
select k.Keyword.Id;
var matchingKac = from kac in keywordAdCategoryQuery
where keywordIds.Distinct().Contains(kac.Keyword_Id)
select kac;
var addIDs = from kac in matchingKac
group kac by kac.Ad_Id into d
where d.Count() == splitKeywords.Count()
select d.Key;
var groupedKac = from kac in keywordAdCategoryQuery
where addIDs.Contains(kac.Ad_Id) <--- EDIT2
group kac by new { kac.Category_Id, kac.Ad_Id };
var result = from grp in groupedKac
group grp by grp.Key.Category_Id into final
join c in categoryQuery on final.Key equals c.Id
select new CategoryGetAllBySearchDto
{
Id = final.Key,
Name = c.Name,
ListController = c.ListController,
ListAction = c.ListAction,
SearchCount = final.Count()
};
The problem is that I can't get only the Ads that match all Keywords.
EDIT:
When a keyword is made of 2 or more KeywordSearches like "Mercedes-Benz", the line "where d.Count() == splitKeywords.Count()" fails, because d.count = 1 and splitkeywords.Count = 2 for "Mercedes-Benz"
Any Help?
this may not be the direct answer, but in such "multiple parameter search" situations i just forget about anything and do the simple thing, for ex: Search By Car Manufacturer, CategoryId, MillageMax, Price :
var searchResults = from c in carDb.Cars
where (c.Manufacturer.Contains(Manufacturer) || Manufacturer == null) &&
(c.CategoryId == CategoryId || CategoryId == null) &&
(c.Millage <= MillageMax || MillageMax== null) &&
(c.Price <= Price || Price == null)
select c
now if any of the parameters is null it cancels the containing line by making the whole expression in brackets True and so it does not take a part in search any more
If you try to make your own search engine you will probably fail.Why don't you try Lucene.
Here's a link http://lucenenet.apache.org/.
Cheers
I think I have a solution now. This is based on your previous question and a few assumptions:
Keywords are complete names like "Mercedes-Benz GLK", "Mercedes-Benz Citan".
KeywordSearchs are "Mercedes", "Benz" and "GLK" for "Mercedes-Benz GLK" and "Mercedes", "Benz" and "Citan" for "Mercedes-Benz Citan"
"Mercedes-Benz GLK" is a "Car", "Mercedes-Benz Citan" is a "Truck"
With those three assumptions in mind I can say that
var keywordIds = from k in keywordSearchQuery
where splitKeywords.Contains(k.Name)
select k.Keyword.Id;
is the culprit and all queries below rely on it. This query will find all keywords that contain any words in your searchstring.
Example: Given searchstring "Mercedes-Benz GLK" will be split into "Mercedes", "Benz" and "GLK". Your query now finds "Mercedes" and "Benz" in both "Mercedes-Benz GLK" and "Mercedes-Benz Citan".
I think it's obvious that you don't want "Mercedes-Benz GLK" to match "Mercedes-Benz Citan".
The solution is to tell the query to match every splitKeywords with any Keywordsearch and return the appropriate Keyword:
var keywordIds = keywordSearchQuery
.GroupBy(k => k.Keyword.Id)
.Where(g => splitKeywords.All(w =>
g.Any(k => k.Name.Contains(w))))
.Select(g => g.Key);
As for addIds changing it to var addIDs = matchingKac.Select(ad => ad.Ad_Id).Distinct(); should do the trick. Or if matchingKac is only needed in addIds then you could change it to
var matchingKac = (from kac in keywordAdCategoryQuery
where keywordIds.Distinct().Contains(kac.Keyword_Id)
select kac.Ad_Id).Distinct();
and remove addIds.
I haven't compile-checked this or anything, so it may require some tweaking, but you're looking for something along these lines.
var matchingKac = keywordIds.Distinct().ToList()
.Aggregate(
keywordAdCategoryQuery.AsQueryable(),
(q, id) => q.Where(kac => kac.Keyword_Id == id));
You're effectively saying, "Start with keywordAdCategoryQuery, and for each keyword add a .Where() condition saying that it must have that keyword in it. You could do the same thing with a for loop if you find Aggregate difficult to read.
I am suggesting you to add regex and omit that special characters and then use Linq for that
So Mercedez-Benz can become Mercedez and benz
I recommend to NOT define keywords to objects that way, because you might search and find too many objects or you'll find possibly nothing. You will always spoil your time when searching. Classify your objects in a way that the users focus is to FIND and not to search.
I have posted my answer to: https://github.com/n074v41l4bl34u/StackOverflow19796132
Feel free to review it.
Here is the most important snippet.
with:
internal class SearchDomain
{
public List<Keyword> Keywords { get; set; }
public List<Category> Categories { get; set; }
public List<KeywordAdCategory> KeywordAdCategories { get; set; }
}
then:
private static char[] keywordPartsSplitter = new char[] { ' ', '-' };
internal static Dictionary<Category, Dictionary<int, List<KeywordAdCategory>>> FromStringInput(string searchPhrase, SearchDomain searchDomain)
{
var identifiedKeywords = searchPhrase
.Split(keywordPartsSplitter);
var knownKeywordParts = identifiedKeywords
.Where
(ik =>
searchDomain
.Keywords
.SelectMany(x => x.GetKeywordParts())
.Any(kp => kp.Equals(ik, StringComparison.InvariantCultureIgnoreCase))
);
var keywordkSearches = knownKeywordParts
.Select((kkp, n) => new KeywordSearch()
{
Id = n,
Name = kkp,
Keyword = searchDomain
.Keywords
.Single
(k =>
k.GetKeywordParts()
.Any(kp => kp.Equals(kkp, StringComparison.InvariantCultureIgnoreCase))
)
});
var relevantKeywords = keywordkSearches
.Select(ks => ks.Keyword)
.Distinct();
var keywordAdCategoriesByCategory = searchDomain.Categories
.GroupJoin
(
searchDomain.KeywordAdCategories,
c => c.Id,
kac => kac.Category_Id,
(c, kac) => new { Category = c, AdKeywordsForCategory = kac }
);
var relevantKeywordAdCategories = keywordAdCategoriesByCategory
.Where
(kacbk =>
relevantKeywords
.All
(rk =>
kacbk
.AdKeywordsForCategory
.Any(kac => kac.Keyword_Id == rk.Id)
)
);
var foundAdsInCategories = relevantKeywordAdCategories
.ToDictionary
(rkac =>
rkac.Category,
rkac => rkac.AdKeywordsForCategory
.GroupBy(g => g.Ad_Id)
.ToDictionary(x => x.Key, x => x.ToList())
);
return foundAdsInCategories;
}
It does exactly what you want however I find something fishy about keywords being divisible to sub-keywords. Than again, maybe it is just the naming.
Im trying to compute a boolean field based on a subquery
var dtfs = cntx.Set<Models.DocTypeField>()
.Include(dtf => dtf.Field)
.Where(dtf => dtf.DocTypeId == docTypeId)
.Select(dtf => new
{
DocTypeField = dtf,
HasData = (cntx.Set<Models.DocumentData>()
.Any(dd => dd.DocTypeId == dtf.DocTypeId
&& dd.DataValues.Any(ddv => ddv.FieldId == dtf.FieldId)))
});
There is no navigation property(or traversable path) between DocTypeField and DocumentData. When I run the query above I get the following exception:
Test method
Core.Sebring.DataAccess.Ef.Test.EF_DocTypeDALTest.EF_DocTypeDALTest_GetDocTypeIndexes
threw exception:
System.NotSupportedException:
LINQ to Entities does not recognize the method
'System.Data.Entity.DbSet`1[Core.Sebring.Models.DocumentData]
Set[DocumentData]()' method, and this method cannot be translated
into a store expression.
Is there a linq to entity way of accomplishing the above query? I would rather not add a navigation property(or traversable path) between DocTypeField and DocumentData, if possible.
*UPDATE 1*
As a work around I did
class FieldDocTypeField
{
public int DocTypeFieldId { get; set; }
public int DocTypeId { get; set; }
public int FieldDataType { get; set; }
public int FieldId { get; set; }
public byte[] LastChanged { get; set; }
public bool Required { get; set; }
public string FieldName { get; set; }
public bool HasData { get; set; }
}
var dtfs = cntx.DbContext.Database.SqlQuery<FieldDocTypeField>(#"select dtf.*,f.*,
HasData = (CASE WHEN EXISTS(Select DocumentDataValue.FieldId
from DocumentData
inner join DocumentDataValue on DocumentData.DocumentDataId=DocumentDataValue.DocumentDataId
where DocumentData.DocTypeId = #DocTypeId AND dtf.FieldId = 1) THEN cast(1 as bit) ELSE cast(0 as bit) END)
from DocTypeField dtf
inner join Field f on dtf.FieldId = f.FieldId WHERE dtf.DocTypeId=#DocTypeId", new System.Data.SqlClient.SqlParameter("#DocTypeId", docTypeId));
foreach (var dtf in dtfs)
{
docTypeFields.Add(new Models.DocTypeField
{
DocTypeFieldId = dtf.DocTypeFieldId,
DocTypeId = dtf.DocTypeId,
FieldDataType = dtf.FieldDataType,
FieldId = dtf.FieldId,
LastChanged = dtf.LastChanged,
Required = dtf.Required,
FieldName = dtf.FieldName,
HasData = dtf.HasData
});
}
Its not so nice but it works and accomplishes the same things. I could not find a way to do the above using linq to entities without adding a nav property between DocTypeField and DocumentData entities.
You can pull the data in memory first and then do the 2nd select.
var dtfs = cntx.Set<Models.DocTypeField>()
.Include(dtf => dtf.Field)
.Where(dtf => dtf.DocTypeId == docTypeId)
.ToList() // pull the data to memory and then the following select can execute successfully.
.Select(dtf => new
{
DocTypeField = dtf,
HasData = (cntx.Set<Models.DocumentData>()
.Any(dd => dd.DocTypeId == dtf.DocTypeId
&& dd.DataValues.Any(ddv => ddv.FieldId == dtf.FieldId)))
});