Querying nested collection in RavenDb document - c#

I have a filter method using Lucene extensions for a list of Store documents with the following structure:
public class Store {
public string Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Address { get; set; }
public ICollection<Product> Products { get; set; }
}
public class Product {
public string Name { get; set; }
public double Price { get; set; }
}
my filter method:
public IEnumerable<Store> Filter(string term = null)
{
var query = session.Advanced.DocumentQuery<Store>();
if (!string.IsNullOrEmpty(term))
{
var search = $"*{term}*";
var escapeQueryOptions = EscapeQueryOptions.AllowAllWildcards;
query = query
.Search(n => n.Name, search, escapeQueryOptions)
.OrElse()
.Search(n => n.Url, search, escapeQueryOptions)
.OrElse()
.Search(n => n.Address, search, escapeQueryOptions);
}
return query;
}
Now I would like to allow the filter by product's name but I'm unable to figure out how to express this with lucene.

Related

SQL Query to Linq Query Result into complex object

I have SQL Query ready, and that I want its result into a complex SQL object. I want to use Linq to achieve the result.
public class VMPackageList
{
public string PackageName { get; set; }
public string ShortTitle { get; set; }
}
public class VMPackageItenary
{
public string PackageName { get; set; }
public string Day { get; set; }
public string Title { get; set; }
public string Detail { get; set; }
}
public class VMPackageHighlight
{
public string PackageName { get; set; }
public string Highlightname { get; set; }
public string HighlightDesc { get; set; }
}
The expected result in below class
public class VMPackageDetails
{
public VMPackageList vmPackage { get; set; }
public VMPackageItenary[] vmPackageItenary { get; set; }
public VMPackageHighlight[] vmPackageHighlights { get; set; }
}
Below are the SQL query and its result, the same way I want to get into
SQL table data query
This is my result query
I had tried with below code to achieve but I did not get success
var packages = packageRepository.Table;
var highlights = packageHighlightRepository.Table;
var itenaries = packageItenaryRepository.Table;
var data = (from package in packages
join highlight in highlights on package.PackageName equals highlight.PackageName
join iteratory in itenaries on package.PackageName equals iteratory.PackageName //&&
where package.PackageName == packageName //&& highlight.PackageName equals iteratory.PackageName
select new VMPackageDetails
{
// vmPackage = package
}).ToList();
Can anyone help me to get the result?

Mongo DB query collection by subcollection attribute

I have:
public class Movie : IMongoEntity
{
public ObjectId Id { get; set; }
public string Title { get; set; }
public string Year { get; set; }
public List<Actor> Actors { get; set; }
}
public class Actor : IMongoEntity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
If i want to retrieve the entire movie collection I do
var query = this.MongoConnectionHandler.MongoCollection.FindAllAs<Movie>();
No I want to retrieve just the movies that have an actor with a certain name
I've tried something like:
IMongoQuery query = Query<Movie>.Where(m => m.Actors.Select(a => a.Name).Any(n => n.Contains(actorName)));
var exc = this.MongoConnectionHandler.MongoCollection.Find(query);
But that won't work.
I think this should do the trick:
var query = Query<Movie>.ElemMatch(m => m.Actors, builder => builder.EQ(actor => actor.Name, actorName));
var exc = this.MongoConnectionHandler.MongoCollection.Find(query);

Find object with in class using LINQ

I want to return the item that has the profile ID I send. So in order to do this I will need to loop through all of the Items -> WebProproperties -> profile. The Class structure is at the end of the question.
I would rather use LINQ than create a nested foreach. I have been trying to get this to work for more than an hour now. I am stuck.
My first idea was to simply use where. But that doesn't work because you need to have something on the other side that needs to equal.
this.Accounts.items.Where(a => a.webProperties.Where(b => b.profiles.Where(c => c.id == pSearchString)) ).FirstOrDefault();
My second idea was to try using Exists which I don't have much experience with:
Item test = from item in this.Accounts.items.Exists(a => a.webProperties.Exists(b => b.profiles.Exists(c => c.id == pSearchString))) select item;
This doesn't work either:
Could not find an implementation of query pattern for source type 'Bool'
public RootObject Accounts {get; set;}
public class RootObject
{
public string kind { get; set; }
public string username { get; set; }
public int totalResults { get; set; }
public int startIndex { get; set; }
public int itemsPerPage { get; set; }
public List<Item> items { get; set; }
}
public class Profile
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class WebProperty
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
public string internalWebPropertyId { get; set; }
public string level { get; set; }
public string websiteUrl { get; set; }
public List<Profile> profiles { get; set; }
}
public class Item
{
public string id { get; set; }
public string kind { get; set; }
public string name { get; set; }
public List<WebProperty> webProperties { get; set; }
}
You can use Any() to determine existence. Also, note that many of the extension methods have overloads which take a predicate, including FirstOrDefault():
this.Accounts.items.FirstOrDefault(a => a.webProperties
.Any(b => b.profiles
.Any(c => c.id == pSearchString)));
You are looking for the .Any() operation I think. This will return true/false for whether there are any items matching your query.
For example:
if (this.Accounts.Items.Any(i=>i.webProperties.Any(wp=>wp.profiles.Any(p=>p.id == MySearchId)));
EDIT: You have full answer (was posted while I was composing mine) and as pointed out in comments my answer isn't actually returning your found item, just letting you know whether there is one. You can rework the first .Any to be a .FirstOrDefault to get that match.
E.g.
var result = this.Accounts.Items.FirstOrDefault(i=>i.webProperties.Any(wp=>wp.profiles.Any(p=>p.id == MySearchId)))
You can use the below mentioned code.
var abc = rr.items.Where(p => p.webProperties.Any(c => c.profiles.Any(d => d.id == "1"))).FirstOrDefault();
Just for your reference, your class should look like:
public class RootObject
{
public string kind { get; set; }
public string username { get; set; }
public int totalResults { get; set; }
public int startIndex { get; set; }
public int itemsPerPage { get; set; }
private List<Item> _items=new List<Item>();
public List<Item> items
{
get { return _items; }
set { _items = value; }
}
}

c#. EF entity sql. How to get entity with related objects?

I have made simple model for example.
public class Publisher
{
public int Id { get; set; }
public string Title { get; set; }
public Address Location { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Address
{
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public int LanguageId { get; set; }
public int? PublisherId { get; set; }
}
I need to get publishers with related books. I know how to do it using linq to entities. Is it possible to solve a problem using entity sql?
public class CatalogContext : DbContext {...}
public List<Publisher> GetByCity(string city)
{
var result = new List<Publisher>();
string queryString;
queryString = String.Format(#"SELECT VALUE row(a,b)
FROM CatalogContext.Publishers AS a
join CatalogContext.Books AS b on a.Id = b.PublisherId
WHERE a.Location.City = '{0}'", city);
var rows = ((IObjectContextAdapter)_context).ObjectContext.CreateQuery<DbDataRecord>(queryString).ToList();
return ???
}
Query returns required data but it's List<DbDataRecord> - list of pairs <publisher, book>. How to translate it to list of publishers with filled navigation property "Books"?
Is it possible to write query which directly returns List<Publisher>?
you can do the following:
var result = ObjectContext.Publishers.Include("Books").Include("Locations")
.Where(c => c.Location.City = "SOME_CITY").Select(c => c);
Include - basically joins the table.
Then you can drill down to books by doing the following:
var test = result[0].Books;
Why are you using direct sql command instead of Entity Framework code style?

How to use selectmany in linq?

The following is my linq query
var meetingIndividualQuery = meetingsList.SelectMany(o => o.Attendies.Distinct().Where(x => x.CompanyId == company.CompanyId));
I have the following class
public class Meetings
{
public string IndustryCouncil { get; set; }
public string MeetingType { get; set; }
public string MeetingDescription { get; set; }
public string MeetingDate { get; set; }
public string MeetingHours { get; set; }
public string MeetingHourlyValue { get; set; }
public string MeetingTotal { get; set; }
public List<Individual> Attendies { get; set; }
}
With the above query I am getting the correct list of individaul but how I can I use the same query with the same condition to retrieve the list of Meetings. Can you please provide me any code
Following query will return list of meetings, which have at least one attendee with provided company id:
var query = meetingsList.Where(m => m.Attendies.Any(i => i.CompanyId == company.CompanyId));
You can also apply Distinct to Attendies before verifying Any

Categories

Resources