Obtain multiple object data from LINQ - linq

Little bit stuck strugling with WP7 LINQ sorting. I have a list (Metro) which i sort with LINQ such that i obtain the order according to Line ( which is part of the Metro list objects). But I want more information about my objects from my list than just the Line info. Is it possible to obtain straight into the LINQ to gain addition information that each object in the list Metro has other than just line but still sort it the same way?
var stationByLine = from metro in source
group metro by metro.Line into c
orderby c.Key
select new Group<Metro>(C.key, C);
this.citiesListGropus.ItemsSource = stationByLine;
I read that maybe using Concat i should be able to solve this but not really sure how to do that....
Thanks Krst

It's not clear why you're grouping at all. Why not just use OrderBy?
var stationByLine = source.OrderBy(metro => metro.Line);

Related

Iterating over NEST Buckets

I am trying NEST out and it seems very nice, but I am having some trouble understanding some things.
The response is serialized to an hierarchy of objects. I would like to iterate over it and to create my own structure.
I would be able to do somethings like this (thanks to #Martijn Laarman, who helped me in the GitHub page):
var buckets = result.Aggs.Terms("level_1");
var term = buckets.Items[0].Terms("level_2");
It works, but I would like to have a generic algorithm that parses the response. To do that, I would like to get content independently of the query (if it used terms, range, etc). So I would like to do things like:
var buckets = result.Aggregrations["level_1"];
var term = buckets.Items[0].Aggreggation["level_2"];
Unfortunately the Aggregations collection returns Nest.Bucket and I can't do anything from there.
Is there any way that I can iterate over the result independently on how the query was formed?
Thanks!
For sake of completeness, I was not able to find any way to do so.
I created a parser using a mix of JObects and Dictionary and operated over the JSON response to generate the output I wanted.

Int equals String in Linq

Basically I have this problem:
var item =
from itemCT in listC
join ditemRef in servRef.Repository.GetQuery(efContext).DefaultIfEmpty()
on itemCT.specialStringID equals ditemRef.refID
into DitemRef
The problem is that itemCT.specialStringID is a string ( didnt design it ... can't change it )
Still I am required to make the join work. Any hints ?
Looking for related answers I've tried using SqlFunctions.StringConvert((double)ditemRef.refID without success.
Any hints towards the light at the end of the tunnel helps
Many methods found in the System.Convert class are supported within a LINQ-to-Entities query. Use it to convert one of the two types to the other. Then you'll be able to write your query.

SubSonic 3 Linq Join Problems

using the linqtemplates, I tried getting the linq syntax close to what is in the docs
var query = from c in db.CountyLookups
join s in db.StateLookUps on
c.StateLookupID equals
s.StateLookupID
where c.Name2 == countyName &&
s.Abbr == stateAbbr
select new
{
Latitude = c.Latitude,
Longitude = c.Longitude
};
var result = query.SingleOrDefault();
but when .SingleOrDefault() is called, I get a yellow screen of darn that says:
System.NotSupportedException: The member 'StateLookupID' is not supported
the stack trace ends up at:
SubSonic.Linq.Structure.TSqlFormatter.VisitMemberAccess(MemberExpression m)
the StateLookupID column has underscores in the database and is a regular int pk/fk.
what am I doing wrong?
So apparently VisitMemberAccess has no idea what to do with an int, only string and datetime (starting on line 152 of SubSonic.Linq.Structure.TSqlFormatter). I don't know why this would be called on a join, since a join is usually between an int pk/fk (or guid if you like).
I ended up scrapping the linq query in favor of SubSonic.Query.Select. Here is my new code that works:
var query = db.Select.From<CountyLookup>()
.InnerJoin<StateLookUp>()
.Where(CountyLookupTable.Name2Column)
.IsEqualTo(countyName)
.And(StateLookUpTable.AbbrColumn)
.IsEqualTo(stateAbbr);
I then call ExecuteTypedList and map the results back to my model class. Works like buttah. Just wanted to use linq in this case.
I get this error when I've added properties to my models (the IsValid property as mentioned in ASP.Net MVC 1.0, thanks Rob).
I've had this problem on and off for a bit, and I think I've got it nailed down to the query builder trying to build a query for something that should be done in code, not TSQL.
When it tries to generate the SQL, it descends down the path to generate the TSQL via VisitMemberAccess on a complex type (maybe a another model) but it only knows how to perform operations on datetimes and strings in VisitMemberAccess. I'm sorry if this is a bit incoherent, but I'm trying to get my head around it.
To get around this consider using something like LinqKit AsExpandable prior to any operation which will do the TSQL generation. I've tried this on a simple OrderBy which was going BANG and it appears to work but i have no idea yet what it will do to performance.
Actually I take that back I overcame my problem problem by doing
Stuff.All().Where(x=>x.Someid == id).ToArray()
.AsQueryable()
.Where(x=>x.SomeProp.SomeFlag == true);
It is crud, but it works.
This still appears to be a problem; namely in simple cases such as:
var list = from lang in db.Languages
join site in db.SiteConfigLanguages on
lang.Code equals site.LanguageCode
select lang;
This should evaluate to simple SQL (although pointless in this example):
SELECT Language.* FROM Language LEFT JOIN SiteConfigLanguage ON Language.Code = SiteConfigLanguage.LanguageCode;
It fails inside the same VisitMemberAccess function as (in this case) Language is not a recognisable declaring type (i.e. String or DateTime). It is very similar to the description #matware provided above however it sounds as though the "IsValid" member is pure C# code whereas in this case lang.Code is simply a reference to a column in the database.
I'm currently investigating workarounds as this is only a portion of the larger LINQ query which is failing for me; if I find anything I will post it here. Otherwise, any other solutions/workarounds to this problem known?
UPDATE: Ignore me here; this is simply due to me missing a simple line on the LINQ statement; you need to make sure that you use the "into" keyword to complete things!
i.e.
var list = from lang in db.Languages
join site in db.SiteConfigLanguages on
lang.Code equals site.LanguageCode into sl
from siteLang in sl.DefaultIfEmpty()
select lang;
I've got another error mind you but at least this particular exception is solved. The next one looks a bit nastier unfortunately (inside System.Linq library).

What LINQ features are not supported with SubSonic 3?

What linq features are currently not supported in SS3? Can't seem to find a list.
Ta,
Bob
I think union is one feature not supported. I just ran into this last week, searched and found more info here.
Also, you can't make an non-anonymous select in a custom object. While
from elem in XCollection select new
with {.customField = elem.someProperty}
works well, this doesn't:
from elem in XCollection select new CustomClass _
with {.customField = elem.someProperty}
Left joins are also unsupported.

Using Linq to select a list of Entities, linked Entities, linked Entities

Apologies for the poor question title - I'm not sure how to describe what I'm doing but that is the best I could come up with, please edit it if what I'm asking for has a real name!
I have Programmes, which can have a group of Projects assigned, which in turn have groups of Outputs assigned.
I would like to get all the outputs for the Programme through it's projects as one big list of outputs. I have this:
From pp In Me.ProgrammeProjects Select pp.Project.Outputs
Which basically gets me a list of output lists. (An Ienumerable Of EntitySet Of Output).
I'm brute forcing my way through Linq and can't find any examples of this (or can't recognise one when I see it). How can I do this using just Linq rather than for loops and Linq where I'd go through each EntitySet and add it's contents to a bigger list?
Thanks
Or go against the linq context directly:
from o in context.Outputs
where o.Project.ProgrammeProjects.ID = 1
select o
The reverse will work too and query straight from the data context's table.
Are you trying to get a list of outputs for a specific programme?
If so, try something like this:
var result = (from pp in ProgrammeProjects
where pp.Name.Equals("ProjectA")
select pp.Project.Outputs).ToList();
or
once you get your list of outputs, you could use a lambda expression to get a subset.
var result = (from pp in ProgrammeProjects
select pp.Project.Outputs).ToList();
var subResult = result.FindAll(target => target.OutputParameter.Equals("findThisValue");
Is this what you're trying to do?
If not, give a bit more detail of the data structure and what you're trying to retrieve and I'll do my best to help.
Patrick.
This is the way I've resorted to doing it, but I can tell it's going to be slow when the amount of data increases.
Dim allOutputs As New Generic.List(Of Output)
Dim outputLists = From pp In Me.ProgrammeProjects Select pp.Project.Outputs.ToList
For Each outputList In outputLists
Dim os = From o In outputList Where o.OutputTypeID = Type Select o
allOutputs.AddRange(os)
Next
Return allOutputs
I'm still a bit confused as to what kind of data you're trying to retrieve. Here is what I understand.
You have a list of Programmes
Each Programme can have many Projects
Each Project can have many outputs.
Goal: To find all outputs of a certain type. Is this right?
It doesn't look like you're retrieving any data related to the project or programme so
something like this should work:
Dim allOutputs As Generic.List(Of Outputs) = (From output In Me.Outputs Where output.OutputType.Equals(Type) Select output).ToList()
Let me know how it goes.
Patrick.

Resources