I have two tables:
User
Name
Surname
Phone
number
type_of_number
var myList = ((IRawGraphClient) client).ExecuteGetCypherResults<**i_need_class_to_save_it**>(
new CypherQuery("match (a:User)-[r]->(b:Phone) return a,collect(b)", null, CypherResultMode.Set))
.Select(un => un.Data);
How create correct collection to save data?
It sounds like you haven't read https://github.com/Readify/Neo4jClient/wiki/cypher or https://github.com/Readify/Neo4jClient/wiki/cypher-examples. You really should start there. It helps to read the manual. I don't just write it for fun.
For your specific scenario, let's start with your Cypher query:
MATCH (a:User)-[r]->(b:Phone)
RETURN a, collect(b)
Now, we convert that to fluent Cypher:
var results = client.Cypher
.Match("(a:User)-[r]->(b:Phone)")
.Return((a, b) => new {
User = a.As<User>(),
Phones = b.CollectAs<Phone>()
})
.Results;
You can then use this rather easily:
foreach (var result in results)
Console.WriteLine("{0} has {1} phone numbers", result.User.Name, result.Phones.Count());
You will need classes like these, that match your node property model:
public class User
{
public string Name { get; set; }
public string Surname { get; set; }
}
public class Phone
{
public string number { get; set; }
public string type_of_number { get; set; }
}
Again, please go and read https://github.com/Readify/Neo4jClient/wiki/cypher and https://github.com/Readify/Neo4jClient/wiki/cypher-examples.
Add a .ToList() at the end of your command?
Like this :
var myList = ((IRawGraphClient) client).ExecuteGetCypherResults<**i_need_Collection**>(
new CypherQuery("match (a:User)-[r]->(b:Phone) return a,collect(b)", null, CypherResultMode.Set))
.Select(un => un.Data).ToList();
Related
While using LinQ, retrieving data from foreign key table data are available. But when I try to 'Add' into my ViewModel this warnings shows. Warningsare difference from each other. such as,
Cannot implicitly convert type 'System.Collection.Generic.IEnumerable<string>' to 'string'
and
Cannot implicitly convert type 'System.Collection.Generic.IEnumerable<System.Collection.Generic.IEnumerable.IEnumerable<string>' to 'string'.
I tried casting to ToString() but it was worthless, no errors shows but data was replaced with system messages. I also tried to LinQ Join in students but I was unable to show Skills Comma Separated that way.
Here is my code:
public ActionResult GetStudentsInfo ()
{
var students = (from stud in db.Students
group stud by stud.StudentId into sg
select new
{
studentName=sg.Select(s=>s.StudentName).FirstOrDefault(),
coutryName=sg.Select(c=>c.Country.CountryName),
cityName=sg.Select(ct=>ct.Country.Cities.Select(x=>x.CityName)),
skillName=sg.Select(sk=>sk.StudentSkills.Select(s=>s.Skill.SkillName)),
resumeName=sg.Select(r=>r.Resumes.Select(m=>m.ResumeName)),
dob=sg.Select(d=>d.DateOfBirth)
}).ToList();
List<StudentListVM> studentLists=new List<StudentListVM>();
foreach (var item in students)
{
studentLists.Add(new StudentListVM
{
studentName = item.studentName,
country = item.coutryName, //warnings here
city = item.cityName, //warnings here
skills = string.Join(",", item.skillName),
resume = item.resumeName, //warnings here
dateOfBirth = item.dob //warnings here
});
}
return View(studentLists);
}
```
StudentListVM class
public class StudentListVM
{
public string studentName { get; set; }
public string country { get; set; }
public string city { get; set; }
public string skills { get; set; }
public string resume { get; set; }
public DateTime dateOfBirth { get; set; }
}
```
I tried this before
var students = (from stud in db.Students
join con in db.Countries on stud.CountryId equals con.CountryId
join ct in db.Cities on stud.CityId equals ct.CityId
join rsm in db.Resumes on stud.ResumeID equals rsm.ResumeId
join stsk in db.StudentSkills on stud.StudentId equals stsk.StudentId
//group stud by stud.StudentId into sg
select new StudentListVM()
{
studentName = stud.StudentName,
countries = con.CountryName,
cities = ct.CityName,
skills=stsk.Skill.SkillName,
resumes = rsm.ResumeName,
dateOfBirth = stud.DateOfBirth,
}).ToList();
```
StudentSkill class:
public partial class StudentSkill
{
public int StudentSkillsId { get; set; }
public int StudentId { get; set; }
public int SkillId { get; set; }
public virtual Student Student { get; set; }
public virtual Skill Skill { get; set; }
}
```
This returns All fine except The Skills in a comma separated list. All I need to show my multiple skills that are checked multiply and added to the database in a separated table name StudentSkills. Is there any good solution to do it?
You are trying to assign a group of strings, specifically the various IEnumerable<string> collections, into a single string due to your Select() calls.
For example, this line is clearly selecting more than one resume name.
resumeName=sg.Select(r=>r.Resumes.Select(m=>m.ResumeName))
If you don't care and expect them to be all the same value you could just grab the first one:
resume = item.resumeName.FirstOrDefault()
Or flatten the collection some other way.
That said, there's something off with the design when you grab a collection and try to assign it to a single item.
Try to change this line:
skills = string.Join(",", item.skillName),
With this:
skills = string.Join(",", item.skillName.ToArray()),
The problem you have is not in your code. The problem is how you think you are solving it. Think it right, the solution will be right. #Zer0 already mentioned that but you probably need more explanation. I will try to explain with some assumption that you might be wanting to do --
1: If Student can have multiple country, city, skill and resumes, then the StudentVM class you have is most certainly wrong. By definition it only supports one city, country, skill, etc. Modify it to support multiples -
public class StudentListVM
{
public string studentName { get; set; }
public List<string> countries { get; set; }
public List<string> cities { get; set; }
public string skills { get; set; }
public List<string> resume { get; set; }
//does not make sense to have a list, a person has only one DOB
public DateTime dateOfBirth { get; set; }
}
then the code you have will work -
public ActionResult GetStudentsInfo ()
{
var students = (from stud in db.Students
group stud by stud.StudentId into sg
select new
{
studentName=sg.Select(s=>s.StudentName).FirstOrDefault(),
coutryName=sg.Select(c=>c.Country.CountryName),
cityName=sg.Select(ct=>ct.Country.Cities.Select(x=>x.CityName)),
skillName=sg.Select(sk=>sk.StudentSkills.Select(s=>s.Skill.SkillName)),
resumeName=sg.Select(r=>r.Resumes.Select(m=>m.ResumeName)),
dob=sg.Select(d=>d.DateOfBirth).FirstOrDefault()
}).ToList();
List<StudentListVM> studentLists=new List<StudentListVM>();
foreach (var item in students)
{
studentLists.Add(new StudentListVM
{
studentName = item.studentName,
countries = item.coutryName.ToList(), //should work, as these are lists
cities = item.cityName.ToList(), //should work, as these are lists
skills = string.Join(",", item.skillName),
resume = item.resumeName.ToList(), //should work, as these are lists
dateOfBirth = item.dob //does not make sense to have a list, a person has only one DOB
});
}
return View(studentLists);
}
2: Once the class is okay, you could shorten the code. You don't need a second block to create a typed list, you can do it directly -
public ActionResult GetStudentsInfo ()
{
var students = (from stud in db.Students
group stud by stud.StudentId into sg
select new StudentListVM
{
studentName=sg.Select(s=>s.StudentName).FirstOrDefault(),
countries=sg.Select(c=>c.Country.CountryName).ToList(),
cities=sg.SelectMany(ct=>ct.Country.Cities.Select(x=>x.CityName)).ToList(),
skills=string.Join(",", sg.Select(sk=>sk.StudentSkills.Select(s=>s.Skill.SkillName))),
resume=sg.SelectMany(r=>r.Resumes.Select(m=>m.ResumeName)).ToList(),
//does not make sense to have a list, a person has only one DOB
dob=sg.Select(d=>d.DateOfBirth).FirstOrDefault()
}).ToList();
return View(students);
}
3: If the above does not make sense, then the idea is not right. Think of what you are trying to achieve and update the question. May be then people will be able to help.
You have said -
Actually I can do them separately in different ways but I can't do it in a single code
So what are those ways? Mentioning them will probably give an idea of what you are trying to achieve. The details on the question is not enough to give you solution. It does not say what you are trying to do.
I have an IEnumerable containing values like "ABC", "DEF", etc.
I'm trying to form a LINQ query where the value of a property in my object may be equal to one of the many values contained in my IEnumerable. How do I form that LINQ query?
var statesFilter = new List<string>();
statesFilter.Add("NY");
statesFilter.Add("CA");
var employees = new List<Employee>();
employees = getDataFromSomewhere();
// Code below is not working. Just wanted to give you an idea about my LINQ query
var myFilteredList = employees.Where(x => x.State.Contains(statesFilter));
Employee class could be something like this:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string State { get; set; }
}
If State property is of type string, you should change your Where to the following:
var myFilteredList = employees.Where(x => statesFilter.Contains(x.State));
Also, you can do this:
var myFilteredList = employees.Where(x => statesFilter.Any(s=>s==x.State));
Condition should be
x=>statesFilter.Contains(x.State))
I wonder if someone could spare me a few minutes to give me some advice please?
I've created an IEnumerable list:
public class EmailBlock
{
public int alertCategory { get; set; }
public string alertName { get; set; }
public string alertURL { get; set; }
public string alertSnippet { get; set; } //Need to work out the snippet
}
List<EmailBlock> myEmailData = new List<EmailBlock>();
Which I then loop through some data (Umbraco content - not that that's really relevant!) and add items to the list.
myEmailData.Add(new EmailBlock { alertCategory = category.Id, alertName = alert.GetPropertyValue("pageTitle"), alertURL = alert.NiceUrl });
What ultimately I'd like to do is group the list by the alertCategory and then load each 'group' (another loop occurs later to check what members have subscribed to what alert category) into a variable which I can then use as an email's content.
You could use Linq's GroupBy() to do this:
using System.Linq
...
//Create a type to hold your grouped emails
public class GroupedEmail
{
public int AlertCategory { get; set; }
public IEnumerable<EmailBlock> EmailsInGroup {get; set; }
}
var grouped = myEmailData
.GroupBy(e => e.alertCategory)
.Select(g => new GroupedEmail
{
AlertCategory = g.Key,
EmailsInGroup = g
});
You can select to an anonymous type if required and project your sequence into whatever structure you require.
Linq has a nice group by statement:
var emailGroup = emailList.GroupBy(e => e.alertCategory);
Then you can loop through each grouping and do whatever you want:
foreach(var grouping in emailGroup)
{
//do whatever you want here.
//note grouping will access the list of grouped items, grouping.Key will show the grouped by field
}
Edit:
To retrieve a group after you have grouped them, just use Where for more than one or First for just one:
var group = emailGroup.First(g => g.Key == "name you are looking for");
or
var groups = emailGroup.Where(g => listOfWantedKeys.Contains(g.Key));
this is a lot more efficient than looping through every time you need to find something.
Ugh, how do I explain this one... Probably a simple question but my mind is fried.
Suppose I have this class:
public class NestedObject
{
public string NestedName { get; set; }
public int NestedIntValue { get; set; }
public decimal NestedDecimalValue { get; set; }
}
public class SomeBigExternalDTO
{
public int Id { get; set; }
public int UserId { get; set; }
public int SomeIntValue { get; set; }
public long SomeLongValue { get; set; }
public decimal SomeDecimalValue { get; set; }
public string SomeStringValue { get; set; }
public NestedObject SomeNestedObject { get; set; }
// ... thousands more of these properties... inherited code
}
And the class I'd like to populate is here:
public class MyResult
{
public int UserId { get; set; } // user id from above object
public string ResultValue { get; set; } // one of the value fields from above with .ToString() executed on it
}
What I'd like to do is create a helper to return the property values (a cross section is the best way I could describe it I guess) of all instances in a list of this object:
var foo = new List<SomeBigExternalDTO>();
foo = GetMyListOfSomeBigExternalDTO();
public static List<MyResult> AwesomeHelper(List<SomeBigExternalDTO> input, SearchableProperty thePropertyIWant)
{
// some magic needs to happen here...
}
The tricky part here is I want to dynamically pass in the property based on a link selector (I have no clue how to do this):
var output = AwesomeHelper(GetMyListOfSomeBigExternalDTO(), x => x.SomeIntValue);
var output2 = AwesomeHelper(GetMyListOfSomeBigExternalDTO(), x => x.SomeNestedObject.NestedIntValue);
And this should return a list of MyResult objects with the UserId and SomeIntValue.ToString() corresponding to each item in the input list.
Wow, I really hope this makes sense. Please let me know if this is not clear I'll provide more details. I'm really hoping this is something baked into the libraries that I've overlooked.
Any ideas on I'd accomplish this?
You could implement it as an extension method:
public static IEnumerable<MyResult> AwesomeHelper(this IEnumerable<SomeBigExternalDTO> input,
Func<SomeBigExternalDTO, int> intMapper)
{
foreach (var item in input)
yield return new MyResult()
{
UserId = item.UserId,
ResultValue = intMapper(item)
};
}
Now you can use it like this:
var output = GetMyListOfSomeBigExternalDTO().AwesomeHelper( x => x.SomeIntValue);
var output2 = GetMyListOfSomeBigExternalDTO().AwesomeHelper( x => x.SomeNestedObject.NestedIntValue);
Having said that - dont' do that - it somehow looks like you are reinventing what Linq already offers you, you can do just the same using only Linq:
var output = GetMyListOfSomeBigExternalDTO().Select( x=> new MyResult()
{
UserId = item.UserId,
ResultValue = x.SomeIntValue
});
var output2 = GetMyListOfSomeBigExternalDTO().Select( x=> new MyResult()
{
UserId = item.UserId,
ResultValue = x.SomeNestedObject.NestedIntValue
});
Often when trying to create a general purpose list operator you end up reimplementing what LINQ already offers you.
Here's the LINQ code for what you're after (without an AwesomeHelper function):
var results = list.Select(l => new MyResult()
{
UserId = l.UserId,
ResultValue = l.SomeDecimalValue.ToString()
}).ToList();
Fairly simple.
If you want to have an AwesomeHelper function as you requested then it looks like this:
public static List<MyResult> AwesomeHelper(
List<SomeBigExternalDTO> input,
Func<SomeBigExternalDTO, object> selector)
{
return input
.Select(i => new MyResult()
{
UserId = i.UserId,
ResultValue = selector(i).ToString()
})
.ToList();
}
And the calling code look like this:
var results = AwesomeHelper(list, x => x.SomeIntValue);
To me, though, this is now less readable than the LINQ option. Now there is some magic being wrought and it's hard to work out what.
I have an alternative that will give you the best of both worlds.
First, define an extension method called ToMyResult that maps a single SomeBigExternalDTO instance into a single MyResult with a field selector, like this:
public static class AwesomeHelperEx
{
public static MyResult ToMyResult(
this SomeBigExternalDTO input,
Func<SomeBigExternalDTO, object> selector)
{
return new MyResult()
{
UserId = input.UserId,
ResultValue = selector(input).ToString()
};
}
}
Now the calling code is crystal clear, flexible and concise. Here it is:
var results = (
from item in list
select item.ToMyResult(x => x.SomeLongValue)
).ToList();
I hope this helps.
I have a IQueryable<SomePOCO> (a LINQ-Entities query, if that matters):
public class SomePOCO
{
public string ParentName { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
And i'm trying to project to a single object (anonymous type would be best, since i only need method scope) which has 2 properties:
public string ParentName { get; set; }
public ICollection<SimplePoco> { get; set;
SimplePOCO is as follows:
public class SimplePOCO
{
public string Name { get; set; }
public string Url { get; set; }
}
The reason i'm doing this is that all of the "SomePOCO"s im retrieving have the same ParentName, so i just want that once, as opposed to bringing over the wire the same value N amount of times and doing a .First().
Hope that makes sense.
The end result is i should be able to do this:
var parentName = result.ParentName; // string
var pocos = result.SimplePOCOs; // ICollection<SimplePOCO>
I think i either need some kind of aggregation, like with GroupBy or SelectMany.
Any ideas?
I think you just need to do a group by Parent Name
var result = collection.GroupBy(i => i.ParentName)
.Select(g => new {
ParentName = g.Key,
SimplePocos = g.Select(i => new SimplePoco
{
Name = i.Name,
Url = i.Url
})
});
This is the first step.
var groups = myQ.GroupBy(p => p.ParentName);
You will need to have your middle data structure. I'll call it Grouping.
var myList = new List<Grouping>();
foreach (var group in groups) {
var newGrouping = new Grouping();
new Grouping.ParentName = group.Key;
newGrouping.SimplePocos = group.Select(p => new SimplePoco(p)).ToArray();
}
You should have a constructor of SimplePoco that will convert it for you.