No overload for method 'Join' takes 1 arguments - c#

What he wants me to do here for Join can you help me solve the problem?
this.Parts
.Join(this.PartInformation, p => p.PartId, pi => pi.PartId, (p, pi) => new
{
p.ListId,
p.PartId,
pi.PartDescription
});

I think you want the static string.Join method instead:
.ToDictionary(x => x.Key, x => string.Join(";", x.Select(a => a.UrlPrefix)))

Your method signature suggests you need to use string.Join:
var result = data
.GroupBy(x => x.DomainId)
.ToDictionary(x => x.Key, x => string.Join(";", x.Select(a => a.UrlPrefix)));

Related

using substring and foreach in a lambda expression

Dictionary<int, string> names= GetNames().Where(x =>
x.Value.StartsWith("test") | x.Value.StartsWith(" " + "test")).ToDictionary(x => x.Key, y => y.Value);
The values from the getNames() method are something like this:
John Testing
Test Travis
Test Metsaoy
Using the above line of code I'm getting only the two last entries, but I want also the 1st one because the 2nd string starts with "test".
So, I need to modify the above where statement. I tried something like this:
.Where(x =>
foreach ( xx in x.Value.Split(' ') ) { if ( xx.StartsWith("text") ) true; })
How can I achieve this?
var res = GetNames().Where(kvp => kvp.Value.Split()
.Any(s => s.StartsWith("Test") || s.StartsWith("test")));
Optionally instead of StartsWith you can use String.Contains in the Any lambda.
Try:
var parser = new Regex(#"\bTest", RegexOptions.Compiled);
GetNames().Where(x => parser.IsMatch(x.Value)).ToDictionary(x => x.Key, y => y.Value)
Have you tried this?
x.Value.StartsWith("test") || x.Value.Contains(" test")
You'll have to use it in your query like this:
var names= GetNames()
.Where(x => x.Value.StartsWith("test") || x.Value.Contains(" test"))
.ToDictionary(x => x.Key, y => y.Value);
Hope it helps

Ordered grouping without Dictionary

I am grouping train trips according to the coal mines they service, and then dealing with the each group starting with the most populous.
List<List<Trip>> tripsByMine = trips.GroupBy(x => x.demand.Mine)
.ToDictionary(x => x.Key, x => x.ToList())
.Values.OrderByDescending(x => x.Count)
.ToList();
It seems to me that the ToDictionary call is superfluous because I just want the Values. Is there a shorter way of getting the same result?
Try this
List<List<Trip>> tripsByMine2 = trips.GroupBy(x => x.demand.Mine)
.Select(x => x.ToList())
.OrderByDescending(x => x.Count)
.ToList();
Possible solution:
List<List<Trip>> tripsByMine = trips.GroupBy(x => x.demand.Mine)
.Select(x => new {Key=x.Key, Values=x,Count=x.Count()})
.OrderByDescending(x => x.Count)
.Select(x=>x.Values.ToList())
.ToList();

NHibernate: Could not resolve property error

I am trying to do a query like:
var a = session.QueryOver<Site>()
.SelectList(
x => x.Select(p => p.SiteName)
.Select(p => p.SiteId).Select(p => p.RegionLocation.City))
.List<object[]>();
but I get the error
could not resolve property: RegionLocation.City of: Entities.Site
The property exists and I can retrieve it using LINQ but QueryOver does not work. What am I doing wrong ?
As far as i remember, with QueryOver, you have to join all entities in the association in order to be able to access it's properties.
This means you ought to do something like:
(notice the .JoinQueryOver)
var a = session.QueryOver<Site>()
.JoinQueryOver(s => s.RegionLocation)
.SelectList(
x => x.Select(p => p.SiteName)
.Select(p => p.SiteId)
.Select(p => p.RegionLocation.City))
.List<object[]>();
Or maybe this will work:
RegionLocation regionLocationAlias = null;
var a = session.QueryOver<Site>()
.JoinAlias(s => s.RegionLocation, () => regionLocationAlias)
.SelectList(
x => x.Select(p => p.SiteName)
.Select(p => p.SiteId)
.Select(() => regionLocationAlias.City))
.List<object[]>();
Also you might want to have a look at https://github.com/davybrion/NHibernateWorkshop/tree/master/NHibernateWorkshop
There's lots of great examples!
Specifically for your problem, have a look at: https://github.com/davybrion/NHibernateWorkshop/blob/master/NHibernateWorkshop/Querying/QueryOver/Projecting.cs

Return max repeated item in list

List<string> prod = new List<string>();
prod.Add("dfg");
prod.Add("dfg");
prod.Add("ojj");
prod.Add("dfg");
prod.Add("e");
In the above code prod List has item "dfg" repeated thrice(max count)...
I want "dfg" as the output because this item is repeated maximum times.
Can anyone help in this
Not the absolutely most efficient, but it works:
var maxRepeatedItem = prod.GroupBy(x => x)
.OrderByDescending(x => x.Count())
.First().Key;
This is more efficient:
var maxRepeatedItem = prod.GroupBy(x => x)
.MaxBy(x => x.Count())
.First().Key;
but it requires MoreLinq's extension MaxBy
EDIT (as per comment) :
If you want all the max repeated elements in case of ties, here's a possible solution:
var grouped = prod.ToLookup(x => x);
var maxRepetitions = grouped.Max(x => x.Count());
var maxRepeatedItems = grouped.Where(x => x.Count() == maxRepetitions)
.Select(x => x.Key).ToList();
You can use LINQ:
string maxRepeated = prod.GroupBy(s => s)
.OrderByDescending(s => s.Count())
.First().Key;

Why can I not use OrderBy() in this lambda expression?

How do I order the following? The orderBy doesnt recognise the x.Name.
var xRefsNames = db.CrossRefs.Where(x => pgNos.Contains(x.PG))
.Select(x => x.Name)
.Distinct()
.OrderBy(x=>x.Name);
Your select is projecting a different object, probably a string based on the name. You want to just order by x.
var xRefsNames = db.CrossRefs.Where(x => pgNos.Contains(x.PG))
.Select(x => x.Name)
.Distinct()
.OrderBy(x=>x);

Categories