Linq Group On Multiple Fields - Anonymous - Key
Apr 3, 2012I am stumped. I have a DTO object with duplicates patient address data. I need to get only the unique addresses.
[Code]...
I am stumped. I have a DTO object with duplicates patient address data. I need to get only the unique addresses.
[Code]...
My LINQ query contains the following Group By statement:
Group p By Key = New With { _
.Latitude = p.Address.GeoLocations.FirstOrDefault(Function(g) New String() {"ADDRESS", "POINT"}.Contains(g.Granularity)).Latitude, _
.Longitude = p.Address.GeoLocations.FirstOrDefault(Function(g) New String() {"ADDRESS", "POINT"}.Contains(g.Granularity)).Longitude}
The query works, but here is the SQL that the clause above produces
SELECT [t6].[Latitude]
FROM (
SELECT TOP (1) [t5].[Latitude]
[Code]....
but this produced an error: "A group by expression can only contain non-constant scalars that are comparable by the server."
I'm trying to write a linq to object query in vb.net, here is the c# version of what I'm trying to achieve (I'm running this in linqpad):
void Main()
{
var items = GetArray(
[code].....
I was toying around with some of the linq samples that come with LINQPad. In the "C# 3.0 in a Nutshell" folder, under Chater 9 - Grouping, there is a sample query called "Grouping by Multiple Keys". It contains the following query:
from n in new[] { "Tom", "Dick", "Harry", "Mary", "Jay" }.AsQueryable()
group n by new
{
[Code].....
The documentation has an example about grouping on multiple properties[code]...
Is it possible to rewrite the original query to just return IEnumerable(Of CustomerRegionGroup), or do I have to use the anonymous type, and run a second query on the result of the first?
I have a list of attachments that I need to group by clientCLID and EmailAddress. From this grouped list I only need a list of clientCLIDs. After fiddling for a while I've managed to get it to work as follows:
[Code]...
I spent a lot of time on this problem. I am able to do simple Group By LINQ queries (on one property) but for multiple fields I'm a little stuck... Here is a LINQPad sample of what I want to do :
dim lFinal={new with {.Year=2010, .Month=6, .Value1=0, .Value2=0},
new with {.Year=2010, .Month=6, .Value1=2, .Value2=1},
new with {.Year=2010, .Month=7, .Value1=3, .Value2=4},
[Code]....
I have the following:
myFilteredContractors = (From c In myFilteredContractors
Join cc In myConClasses On c.ContractorId Equals cc.ContractorId
Where inClassifications.Contains(cc.ClassificationId)[code].....
This is properly ordering this list of contractors by the number of classifications that they have.I also want to order them by whether or not they have a field set (CompanyOverview), which if is an empty string should be below those contractors who have set their CompanyOverview. Also, after the CompanyOverview is ordered I want to order by Registration Date.So it should order by:
Number of Classifications
Whether Overview has been set (c.CompanyOverview)
Registration Date (c.AppliedDate)
Is it possible to all of this in LINQ?
I have a Table:
[Code]...
I'm trying by LINQ-query (VB.NET) to return a count for distinct value of Objects, Stuffs and Colours for every Room:
[Code]...
Can anyone help me by translating this SQL Statement to Linq? [code]
View 1 RepliesI'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures.
[Code]...
I found that when group by multiple values does not work well with VB.NET, but it works well with C# ,here are my code, is there something wrong with my VB.NET Code? Here is my VB.NET code:
[Code]...
Why did the anonymous type property "Points" still have the value "0"? [code]
View 1 RepliesI have a problem using linq and anonymous types in an asp.net vb.net application.When I write the code above, I have 2 errors :
- "declarations of variable with Option Strinct On requires an As clause"
- "the name 'item' is not declared"
Public Sub MyFunc(ByVal xe As System.Xml.Linq.XElement)
Dim lstitems = From it In xe.Elements Select txt = it.@Text, value = it.@Value
For Each item In lstitems
Dim s As String = item.txt
Next
End Sub
Does it mean that is not possible to use Option Strict On and Option Explicit On ?Or I make a syntax error ?Or is it just a bad configuration of my application ?
I'm out of my depth populating a windows form from an XML linq query (see code further down in post). The listbox is populated as intended, when a value is selected (row) I would like to populate the remaining form controls. The linq query I believe has created a new datatype "Anonymous Types". The query is only local to the load form subroutine but I'm not sure how to make it global.
When I define the query simply as an object the code in the selectedValueChanged event can't execute due to late databinding. how I can initially load the listbox from linq xml query and then populate the other controls when the value changes? Am I right in thinking even though the listbox is multi-columns (property) you can ONLY ever reference a row and NOT a column? I'm new to LINQ and Anonymous Types and the more I read the more confused I seem to get. On a less important note the order by is not functioning as intended (no errors) but data is always retrieved in xml file order and not by the node <text>. Also looked at using <text>.first to order the results without success.
[Code]...
I would like to use reflection on an anonymous type resulting from a LINQ to SQL query. I know that reflection on anonymous types works in general. The following code successfully generates a propertyinfo array with two elements:
Dim MyObject = New With {.Col1 = 1, .Col2 = "Test"}
Dim t As Type = MyObject.GetType()
Dim pilist As PropertyInfo() = t.GetProperties
[Code].....
I've long since built a way around this, but it still keeps bugging me... it doesnt help that my grasp of dynamic LINQ queries is still shakey.
For the example:
Parent has fields (ParentKey, ParentField)
Child has fields (ChildKey, ParentKey, ChildField)
Pet has fields (PetKey, ChildKey, PetField)
[Code].....
The above Join call doesnt work. I sort of understand why it doesnt work, but hopefully it'll show you how I tried to accomplish this task.
After all this was done I would have appended a Select to finish the job.
I tried it with the PredicateBuilder with little success. I might not know how to use it right but it felt like it wasnt gonna handle the joining.
Supposing the referenced List below contains 2 elements:
Dim Countries = From c In List _
Select New With { .Country = c.Country, .CountryID = c.CountryID }
The code above returns
.Country=Spain .CountryID = 1
.Country=Spain .CountryID = 1
How can I get the distinct values? The Countries query should contain only
.Country=Spain .CountryID = 1
I am using linq to fill a gridview with the information from an xml from code behind. I would like to order my Grid according to one of my elements in the xml ("value element").
gvResourceEditor.DataSource = (From resElem In resourceElements.Elements("data") _
Select New With { _
.Key = resElem.Attribute("name").Value, _
.Value = HttpUtility.HtmlEncode(resElem.Element("value").Value), _
.Comment = If(resElem.Element("comment") IsNot Nothing, HttpUtility.HtmlEncode(resElem.Element("comment").Value), String.Empty) _
}).OrderBy(?????)
Any best approach to what I'm trying to achieve (linq to sql, returning list of data to show in a grid/list etc etc)... Its complaining about anonymous type conversion, and from what I'm reading, thats not elegant way of doing it.
Public Function GetHistory(ByVal historyId As Integer) As List(Of ?????????)
Using dc As New myDataContext(Application.GetConnection)
Return (From t In dc.ActionTypes, a In t.MyTable Where a.HistoryID = historyId Select a.ActionOn, a.ActionBy, t.Description, a.ImpactedItem, a.ActionDescription).ToList
End Using
End Function
I am in the process of learning LINQ to Objects. I am querying an array of reference elements using LINQ. In the code below, which I have marked with asteriks and PROBLEM, I am having difficulty returning any values for and I suspect its because of the multiple Select arguments.
'Program name:Querying an Array of Invoice Objects
'Created/revised:
'Project description:To demonstrate the use of LINQ to query objects
[Code].....
I've got the below, where I'm grouping a collection by a propery, then wanting to access the properties of my group. Problem I have is with the anonymous typing. Because of option strict I have to give an explicit type but I can't work out what the type should be. The below doesn't compile because t.HeadAccountKey isn't a value (as t has a type of object). So either I need do some casting or my linq selector is wrong,
[Code]....
I'm trying to translate some C# LINQ code into VB.NET and am stuck on how to declare an anonymous type in VB.NET.[code]How do you translate C#'s new { ... } syntax into VB.NET?
View 2 RepliesI have the following Data Transfer Objects defined:
Public Class MemberWithAddressesDTO
Public Property Member_PK As Integer
Public Property Firstname As String
Public Property DefaultAddress As AddressDTO
Public Property Addresses As IQueryable(Of AddressDTO)
End Class
[Code]...
Every LINQ example I have seen for VB.NET anonymous types claims I can do something like this:
[code]...
Now when I go to iterate through the collection(see example below), I get an error that says "Name "x" is not declared. For Each x in Infos It's like VB.NET doesn't understand that Infos is a collection of anonymous types created by LINQ and wants me to declare "x" as some type. (Wouldn't this defeat the purpose of an anonymous type?) I have added the references to System.Data.Linq and System.Data.DataSetExtensions to my project. Here is what I am importing with the class:
[code]...
I'm learning LINQ and VB and just spent an hour trying to access the fields of an anonymous type defined in a linq query. The key (I learned) is to convert the query to a list before you try to iterate through it with a for loop. How to access property of anonymous type in C#?
This does not work: edit (this compiles, but intellisense does not recognize the type)[code]...
I am using vb.net 2005. I am trying to set report groupings of a crystal report at runtime based on user defined options. MSDN says this:
Dim FieldDef As FieldDefinition
FieldDef =
Report.Database.Tables.Item(0).Fields.Item(comboBox1().Text)
Report.DataDefinition.Groups.Item(0).ConditionField = FieldDef
But error shows invalid group number
So I'm writing a query as follows:
Dim assSummary = From a In db.Assignments
Join ur In db.UserRegions
On a.Origin.ID Equals ur.Region.ID
[code]....
In the controller I can return the data easily as follows:
For Each c In assSummary
MsgBox(c.Description & " " & c.AssCount)
Next
If I pass the object through to the view using Viewdata("assSummary") = assSummary, how do I display the data? Every method I've tried results in messages about 'VB$AnonymousType_7(Of Integer,String) and I don't know how to retrieve the data from the anonymous type.
Suppose I want to build a class like
Class MyClass inherit Something
Dim A as string Bim B as integer
End Class When I am writing code, and i want to use thesenew properties or fields, the intellisense shows me the new properties and fields mixed with those inherited. Is there a way to show them all grouped togheter separated from those inherited ( a part giving them a common prefix in the name)?
I have a form on an asp.net (VB) page which has 2 sections.
All of section 1 is always required, but section 2 is optional. However, if section 2 is started, then all fields are required.
I'm stuck as to the logic I need to create this validation.
Here's my code:
<form runat="server">
<div>
<asp:TextBox runat="server" ID="field1a" Text="Name" />
[Code].....