Rendering a dynamic list of labels from a List<String> - cypher

I need to do something similar to:
#Query("MATCH (n:`:#{literal(#label)}`) WHERE n.entityId=$entityId RETURN n")
Mono<VisibilityGroup> findNode(UUID entityId, String label);
but specifying a list of labels in OR:
Mono<VisibilityGroup> findNode(UUID entityId, List<String> labels);
if label is {"A","B"} this should generate:
MATCH (n:A|B) WHERE...
What is the correct syntax to achieve this?

Related

Dynamic fields for my index not coming up in the list of fields

I am trying to get the fields of my index using the below code snippet.
var fieldsList= DocumentStore.DatabaseCommands.GetIndex("IndexName").Fields.ToList();
This is returning a string list with all fields defined in the index except the dynamic fields ( fields returned from _ ).
Here is the Map command for my index.
Map = products =>
from product in product s
select new
{
product.Title,
product.Subject,
product.From,
_ = product.
Attributes.Select(attribute =>
CreateField(attribute.Name, attribute.Value, false, true))
};
That is by design. The list of fields is the static fields that are in your index.
We don't try to find the dynamic ones.

Group By Sum Linq to SQL in C#

Really stuck with Linq to SQL grouping and summing, have searched everywhere but I don't understand enough to apply other solutions to my own.
I have a view in my database called view_ProjectTimeSummary, this has the following fields:
string_UserDescription
string_ProjectDescription
datetime_Date
double_Hours
I have a method which accepts a to and from date parameter and first creates this List<>:
List<view_UserTimeSummary> view_UserTimeSummaryToReturn =
(from linqtable_UserTimeSummaryView
in datacontext_UserTimeSummary.GetTable<view_UserTimeSummary>()
where linqtable_UserTimeSummaryView.datetime_Week <= datetime_To
&& linqtable_UserTimeSummaryView.datetime_Week >= datetime_From
select linqtable_UserTimeSummaryView).ToList<view_UserTimeSummary>();
Before returning the List (to be used as a datasource for a datagridview) I filter the string_UserDescription field using a parameter of the same name:
if (string_UserDescription != "")
{
view_UserTimeSummaryToReturn =
(from c in view_UserTimeSummaryToReturn
where c.string_UserDescription == string_UserDescription
select c).ToList<view_UserTimeSummary>();
}
return view_UserTimeSummaryToReturn;
How do I manipulate the resulting List<> to show the sum of the field double_Hours for that user and project between the to and from date parameters (and not separate entries for each date)?
e.g. a List<> with the following fields:
string_UserDescription
string_ProjectDescription
double_SumOfHoursBetweenToAndFromDate
Am I right that this would mean I would have to return a different type of List<> (since it has less fields than the view_UserTimeSummary)?
I have read that to get the sum it's something like 'group / by / into b' but don't understand how this syntax works from looking at other solutions... Can someone please help me?
Thanks
Steve
Start out by defining a class to hold the result:
public class GroupedRow
{
public string UserDescription {get;set;}
public string ProjectDescription {get;set;}
public double SumOfHoursBetweenToAndFromDate {get;set;}
}
Since you've already applied filtering, the only thing left to do is group.
List<GroupedRow> result =
(
from row in source
group row by new { row.UserDescription, row.ProjectDescription } into g
select new GroupedRow()
{
UserDescription = g.Key.UserDescription,
ProjectDescription = g.Key.ProjectDescription,
SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
}
).ToList();
(or the other syntax)
List<GroupedRow> result = source
.GroupBy(row => new {row.UserDescription, row.ProjectDescription })
.Select(g => new GroupedRow()
{
UserDescription = g.Key.UserDescription,
ProjectDescription = g.Key.ProjectDescription,
SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
})
.ToList();

Lucene.net - query returning unwanted documents

All of my Lucene.net (2.9.2) documents have two fields:
categoryid
bodytext
bodytext is the default field, and is where all of the document's text is stored (using Field.Store.NO , Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS ).
categoryid is just a numeric field stored as text: Field.Store.YES, Field.Index.NOT_ANALYZED
When this query is executed, it only returns documents with that category ID: categoryid:1
However when I perform this query: categoryid:1 foo bar it returns documents from other categories other than 1.
Why is this? And how can I force it to respect the original categoryid:N query term?
Do you want to require all words entered to be present in your matched documents?
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
var queryParser = new QueryParser(Version.LUCENE_30, "bodytext", analyzer);
// This ensures that all terms are required.
queryParser.DefaultOperator = QueryParser.Operator.AND;
var query = queryParser.Parse("categoryid:1 foo bar");
// query = "+categoryid:1 +bodytext:foo +bodytext:bar"

Criteria API - Using UPPER in a ElementCollection

I have a class
#Entity
public class Person{
...
#ElementCollection
private Set<String> tags;
...
}
I want to use the JPA 2.0 Criteria API to search over these tags - but in a case insensitive way. Thus I want to both set the search parameter to UPPER and the column to UPPER (Pseudo-SQL: select p.* from Person p join Tags t on p.id=t.pId where upper(t.name)=upper('searchParameter'); )
Here is my code without the UPPER on the tags:
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaQuery<Person> query = builder.createQuery(Person.class);
Root<Person> root = query.from(Person.class);
return this.em.createQuery(query.select(root).where(
builder.isMember(builder.upper(builder.literal(searchTag)),
root.get(Person_.tags)))).getResultList();
where searchTag is the input parameter.
How can I assign the UPPER to the Person_.tags "Column"?
In other words I want to write this Query with Criteria API:
SELECT p FROM Person p JOIN p.tags t WHERE UPPER(t) = UPPER('searchString')
Thank you
Ok, I finally have the solution:
cQuery.where(
builder.equal(
builder.upper(cQuery.from(Relation.class).join(Relation_.aliase)
.as(String.class)),
builder.upper(builder.literal(alias))
)
);
One has to use the ".as(..)" method.
suburbCriteria = criteriaBuilder.equal(
criteriaBuilder.upper(root.get(Property_.suburb)),
criteriaBuilder.upper(criteriaBuilder.literal(searchBean.getSuburb())));
You need to call upper and literal property name on joined Tags from Person.
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaQuery<Person> query = builder.createQuery(Person.class);
Root<Person> root = query.from(Person.class);
Join<Person, Tag> tags = root.join(Person_.tags);
query.where(builder.isMember(
builder.upper(builder.literal(searchTag)),
builder.upper(builder.literal(tags.get(Tag_.name)))
));
return this.em.createQuery(query).getResultList();
I omitted query.select(root), not sure if it is required.
Tou can also omit builder.literal() on Tag_.name since is String.
If I am wrong somewhere, please edit my answer for further users.
Hope this helps.

linq to sql/xml - generate xml for linked tables

i have alot of tables with alot of columns and want to generate xml using linq without having to specify
the column names. here's a quick example:
users
---------------
user_id
name
email
user_addresses
---------------
address_id
user_id
city
state
this is the xml i want to generate with linq would look like
<user>
<name>john</name>
<email>john#dlsjkf.com</email>
<address>
<city>charleston</city>
<state>sc</state>
</address>
<address>
<city>charlotte</city>
<state>nc</state>
</address>
</user>
so i'm guessing the code would look something like this:
var userxml = new XElement("user",
from row in dc.Users where user.id == 5
select (what do i put here??)
);
i can do this for one table but can't figure out how to generate the xml for a linked table (like user_addresses).
any ideas?
ok found a way to get the xml i want, but i have to specify the related table names in the query...which is good enough for now i guess. here's the code:
XElement root = new XElement("root",
from row in dc.users
where row.user_id == 5
select new XElement("user",
row.AsXElements(),
new XElement("addresses",
from row2 in dc.user_addresses
where row2.user_id == 5
select new XElement("address", row2.AsXElements())
)
)
);
// used to generate xml tags/elements named after the table column names
public static IEnumerable<XElement> AsXElements(this object source)
{
if (source == null) throw new ArgumentNullException("source");
foreach (System.Reflection.PropertyInfo prop in source.GetType().GetProperties())
{
object value = prop.GetValue(source, null);
if (value != null)
{
bool isColumn = false;
foreach (object obj in prop.GetCustomAttributes(true))
{
System.Data.Linq.Mapping.ColumnAttribute attribute = obj as System.Data.Linq.Mapping.ColumnAttribute;
if (attribute != null)
{
isColumn = true;
break;
}
}
if (isColumn)
{
yield return new XElement(prop.Name, value);
}
}
}
}
You need to use a join. Here's one way:
var query = from user in dc.Users
from addr in dc.UserAddress
where user.Id == addr.UserId
select new XElement("user",
new XElement("name", user.Name),
new XElement("email", user.Email),
new XElement("address",
new XElement("city", addr.City),
new XElement("state", addr.State)));
foreach (var item in query)
Console.WriteLine(item);
i have alot of tables with alot of
columns and want to generate xml using
linq without having to specify the
column names.
Not quite sure how you want to achieve that. You need to state the column names that go into the XML. Even if you were to reflect over the field names, how would you filter the undesired fields out and structure them properly without specifying the column names? For example how would you setup the address part? You could get the fields by using this on your User and UserAddress classes: User.GetType().GetFields() and go through the Name of each field, but then what?