.net - In C# Adding SelectMany Extends Linq To A New Monad Type?

Sep 23, 2009

An old Yet Another Language Geek blog post explaining monads describes adding a SelectMany extension method to C# in order to extend the linq syntax to new types.I've tried it in C# and it works. I did a straight conversion to VB.net and it doesn't work. Does anyone know if VB.net supports this feature or how to use it?

Here is the C# code which works:

class Identity<T> {
public readonly T Value;
public Identity(T value) { this.Value = value; }
}

[code]....

View 2 Replies


ADVERTISEMENT

.net - Nullable Nested Objects And The Maybe Monad?

Sep 19, 2011

I'm struggling to implement a maybe monad - which I've called Nullable in this example.

The Nullable Class is implemented as follows: Public NotInheritable Class Nullable(Of TClass)

Private _value As TClass
Private _hasValue As Boolean
Public Shared Function Create(ByVal value As TClass) As Nullable(Of TClass)
Return New Nullable(Of TClass)(value)
End Function

[Code]...

The way the monad is implemented at the moment I would need to issue the following to access a property on the nested child class dim id = MyParentClass.ChildClass.Value.ID

but ideally what I'd like to be able to do is to have the following statement dim id = MyParentClass.ChildClass.Id and if the ChildClass is null then just return a default value for the property type.

I tried implementing this using Default Properties and setting the Value as default via an attribute but it wouldn't compile. Is that going to be possible or perhaps there is a better way of architecting it - or maybe I just haven't 'got' the maybe monad?

View 1 Replies

LINQ To Entities Does Not Recognize The Method [Type] GetValue[Type]?

Mar 14, 2012

I've a simple class like this:

Public Class CalculationParameter{
public Long TariffId{get;set;}
}

In a workflow activity, I've an Assign like this:

(From tariffDetail In db.Context.TariffDetails
Where tariffDetial.TariffId = calculationParameter.TariffId).FirstOrDefault()

Dto is passed to Activity as an Input Argument.It raise following error and I'm wondering how to assign Id.LINQ to Entities does not recognize the method 'Int64

GetValue[Int64](System.Activities.LocationReference)' method, and this method cannot be translated into a store expression.

How can I assign the calculationParameter.TariffId to tariffDetial.TariffId?!

UPDATE:Screen shot attached shows that how I'm trying to assign calculationParameter.TariffId to tariffDetail.TariffId (car.Id = Dto.Id) and the query result should assign to CurrentTrafficDetail object.

View 1 Replies

LINQ To SQL: Unable To Cast Object Of Type 'System.Guid' To Type 'System.String'

Nov 15, 2011

I am getting a really bizarre error and it makes no sense at all.Basically I have a user entity which has a Password property. This property is of Type Binary, the server data type being Binary(512).I am reading this value in and converting it to a byte array. However whenever I run my code I am getting a completely unrelated error of Unable to cast object of type 'System.Guid' to type 'System.String'.This strikes me as very odd since I am not using a property or variable of type string or GUID anywhere in this procedure where it falls over.

The code is below:

Public Shared Function ValidateUser(ByVal username As String, ByVal password As String) As UserValidationMessage
Using db As New EntityMapDataContext
Dim u = From user In db.Users
Where user.Username = username

[code]....

View 3 Replies

VS 2010 Custom Type Array / List With Adding Items And Specifying Custom Type Variable

Jun 21, 2011

I recently downloaded VB 2010 Express so i am quite new to the language, but i have worked with dark basic, which seems to b very similar.Anyway, my problem is that i cannot seem to correctly set out my array of a custom type/class. i am trying to create two arrays, one of available entities and another of selected entities of which the user selects the entities to be dealt with in irrelevant ways. i have creates a button (called test) which adds some entities to the available list and all the entities come back the same value but they shouldnt be as you'll c in the code. i have run the step into command a found that "available.item(#).Name = " will change all of the entity's names.As of current i am working with lists as the array kept asking for a 'new' statement but i couldnt figure out where to put it.[code]

View 7 Replies

Adding Related Records In LINQ?

Mar 12, 2009

Processing an XML file with LINQ to add records into a table in a SQL Server database via a data context. As we are adding records we need to assign the parents in various other tables. Most of the time we can find the existing parent and use it but sometimes we will need to create a new parent.Thought we could do this like this:

Dim defaultPub As publication
defaultPub = New publication With {.name = e..Value}
Dim pub = _Data.publications.Where(Function(s) s.name =

[code].....

View 1 Replies

Linq-to-XML: Adding Enumerations To Objects?

Oct 2, 2010

I'm trying to figure out how to add an enumeration to an object with Linq. For example:

Dim thingBlock = <Things>
<Thing Name="Ish">
<SmallThing>Jibber</SmallThing>

[code].....

View 1 Replies

Adding Name And Score Record - Insert With LINQ

Apr 22, 2010

What I have is a database in a Windows Form (via tableAdapters etc., basically the default stuff VS adds). What I need to do, is add a very simple record of a name and score (the "place" is an auto-number). I have not even been able to tackle sorting the table, because I can not even figure out how to insert...

View 8 Replies

Define A Type In A Linq?

Mar 1, 2010

I have a list(of object) lets say ob1,ob2,ob3 that are classes I want to write a linq-querry that exstract from this list only one particolur object that is different time to time how can I write the linq querry?

dim List as new list(of Object)
list.add(ob1)
list.add(ob2)
list.add(ob3)
dim Result=(from P as ob3.gettype in list select p).tolist

but it does not work?

View 4 Replies

Linq Using Anonymous Type

Nov 12, 2009

Why did the anonymous type property "Points" still have the value "0"? [code]

View 1 Replies

LINQ Query With Sum And Group By Is Adding Decimal Precision

Aug 24, 2009

I am trying to do a simple summary of table data using a LINQ query.a regular SQL Query looks like:[code]This query works fine and returns the correct results.[code]This LINQ query returns the incorrect results some of the time. Off by .01 in most cases.The "value" column is of type Numeric (12,2) in the database.Now when I show the values from the Sum in a msgbox I get extra precision and thus incorrect results as my end result because of rounding.For example I get 835.18 + 54.62 = 889.80960 from the above LINQ query.I am running Visual Studio 2008 9.0.30729.1 SP on Vista 64Bit with .NET 3.5 SP1.

View 3 Replies

.net LINQ Xml Query - Anonymous Type?

Oct 22, 2009

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]...

View 5 Replies

LINQ Variable Declaration Type?

Feb 1, 2010

[code].....

View 4 Replies

Use Custom Type In A LINQ Query?

Jun 4, 2009

If you have custom type, and you wish to assign a List(of T) to that custom type[code]...

View 1 Replies

Variable Is Not Showing Its Type In LinQ?

Aug 28, 2009

Dim mYVariable = From C In dtXMLConverter Select C I am using the above mentioned syntax for getting the rows in the variable named "mYVariable" after issuing the from clause when I am moving my mouse cursor on the variable name, it is showing "Dim mYVariable As Object" instead of showing DataRow or something like that. I can't understand how to solve this problem.

where dtXMLConverter = DataTable

View 2 Replies

.net - LINQ Error Type DBNull Column?

Apr 29, 2011

I am querying a datagridview and it works great unless one of the cells has nothing (dbnull). How to over come this?Exceptions: Operator '=' is not defined for type 'DBNull' and type 'DBNull'.

Dim query = From row As DataGridViewRow In DataGridView1.Rows _
Where row.Cells(SelectedColumnIndex).Value = filter _
And row.Visible = False _
Select row Distinct

View 1 Replies

Get Minimum Value From Type List Generated From Linq To SQL?

Jun 21, 2011

I'm returning a list of database records;

Dim rsPublicChilds As System.Data.Linq.ISingleResult(Of spGetPublicObjectsResult) = Nothing
rsPublicChilds = dc.spGetPublicObject(slintLoginID, lintLanguageID, lintObjectID, lintObjectTypeID, lstrSEOURL, lstrValid)

I get an enumerable list of rsPublicChildObjects that I then convert to an array;

Dim larr_PublicChild As IEnumerable(Of spGetPublicObjectsResult) = rsPublicChilds.toArray()

That then gives me easy access to an array of the objects, so I can then do;

larr_publicchild(0).colMyValue

etc.etc

I'd like to get the minimum value of colMyValue (or any other property of the object that's been created for me) but I can't quite see how to get there.

View 1 Replies

Group By Over Anonymous Type With Linq To Object?

Feb 25, 2010

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].....

View 1 Replies

LINQ - Order By Anonymous Type (Value Element)

Jan 5, 2012

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(?????)

View 1 Replies

Return Type For Collection From LINQ Query?

Oct 15, 2010

I have a method, that returns a group of accounts

Public Shared Function GetAllNotesByUser(ByVal UserID As Guid) As Account (??)
Using db As New MyEntity
Dim query= (From A In db.Account _

[Code].....

I would then like to pass this to another function to calculate the totals for all the accounts in the collection. Is it best practice to return an Ienumerable, a generic list, I'm just not sure what works best with LINQ and the entity framework.

View 4 Replies

.net - Linq, VB - Anonymous Type Cannot Be Converted To Anonymous Type?

Jul 9, 2009

I'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]...

View 2 Replies

.net - In Linq To Entities - How To Do An Inner EXISTS (SELECT 1 FROM - Type Of Query

Mar 6, 2011

I am trying to write a LINQ equivalent of

SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
EXISTS (SELECT 1 FROM T2
WHERE T1.C17 = T2.C24)
ORDER BY C3

I'm using EF CTP 5, so I have a DBContext variable named dbc, which includes DBSet objects T1s and T2s, based on POCOs T1 and T2.

In LINQ I write

DIM IND = From i In dbc.T1s
Where i.C4 = "xyz"
And (From t In dbc.T2s Where i.C17 = t.C24).Any
Select i.C1, i.C2, i.C3
Order By C3

Running the query I get the error message "Unable to create a constant value of type 'T2'. Only primitive types ('such as Int32, String, and Guid') are supported in this context." When I omit the inner expression (third line in the LINQ code), the query runs fine. I tried switching the orders of the inner comparison, to be t.C24 = i.C17, with no effect.

View 1 Replies

Determine The Type Of Object To Use To Store A Linq Query?

Jul 3, 2010

I am using VB .Net for this, so I don't have access to var or this would be a simple matter.Right now my query is as follows

[code]...

So I used this query in LinqPad to help me determine what the object would look like. I got back a IOrderQueryable(Of RSError) which then contained a IGrouping(Of String, RSError) for each grouped collection of objects returned by the query.However I ended up with the current object type of errors as IOrderedQueryable(Of IGrouping(Of String, RSError)) because of the cast error I am getting in VS.

[code]...

I'm not sure how to get rid of the VB$AnonymousType_1 Part of the returned object.Am I even on the right track here or am I missing something completely?

View 1 Replies

Linq Inheritance And Subtypes (filter Out Base Type)

Nov 8, 2010

OK I have a base query that is returning a list (ObjectQuery, actually but I don't think it matters) I have about 5 subtypes based on the main type and I need to filter them. Everything works fine until I want the results to be of the main type (not a subclass) Basically I have a query that includes everything, then I filter out the types that are not checked in a checkedbox list. (asp.net) Lets say I have an Entity named Task, and SubTask1 and SubTask2 that inherit from Task

[Code]...

View 2 Replies

Returning Subset Of Dictionary With A Complex Type As A Key With Linq

Oct 5, 2010

I have a collection as follows

Private _bankRates As Dictionary(Of RateSourceBank.Key, RateSourceBank)

Where RateSourceBank.Key is simply

Public Class Key
Public RateType As String
Public EffectiveDate As DateTime

[Code].....

View 1 Replies

Sql - Linq - Value Of Type 'System.Guid' Cannot Be Converted To 'String'

Mar 10, 2011

I'm using Linqer to convert SQL to Linq: Update EmployeeSite SET SiteId = Null

[Code]....

View 2 Replies

.net - Linq - Unexpected Uniqueness Constraint-type Behavior Occurring?

Mar 27, 2012

I have a database with a simple flyweight table referenced by another table. Let's call them, respectively, Category and Product.The Product table has several columns, one of which is a foreign key column (complete with constraint) to the CategoryID to a row of the Category table. It's simple, and I have many of them in this particular database.

I am using Linq in Visual Studio 2008 to represent this data in my application. Both tables appear as normal, and neither have anything special in their properties that would indicate the behaviour I'll describe.In the database itself, there are several Product rows which have CategoryID=1. There are two or three that have CategoryID=2. However, when the data is loaded into my application through Linq, iterating through context.Products shows that the first three have CategoryID=1, CategoryID=2 and CategoryID=3, and all the rest of the rows have CategoryID=NULL. This is not how it appears in the database. There are no other anomalies in this database.

View 1 Replies

.net - Pass An Expression To Act On A Single Field In An Anonymous Type In LINQ?

Mar 27, 2012

I 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]...

View 1 Replies

How To Use The Vb Equivalent Of ++ For An Index Property In A Linq Query Projecting A New Type

May 3, 2011

I'd normally do this in C# but since I've got to get this code in this particular assembly which is a vb.net one, I'm stuck.

Here's my linq query:

Dim i As Integer = 0

Dim oldAndCurrentIntersectionOnNames = From currentApplicant In currentApplicants _
Group Join oldApplicant In oldApplicants _

[CODE]...

You'll see the .Index = i+=1

This was my attempt to do what I'd quite happily do in C# (i.e. Index = i++) in VB. Unfortunately the VB compiler doesn't like that. how I'd do this in VB.

View 2 Replies

LINQ Return Single Type Of Object Invalid Cast

Jun 29, 2010

Ok, just needing a 2nd set of eyes looking at this to make sure the error isn't something else other than my LINQ code here. Here's the function class itself:

Public Function GetJacketByPolicyID(ByVal jacketID As Int32) As tblPolicy
Dim db As New DEVDataContext()
Dim j As tblPolicy = db.tblPolicies.Single(Function(p) p.policyNumber = jacketID)
Return j
End Function

and here is the code which calls this class function in the web control form itself:

Dim p As tblPolicy
Dim j As New Jackets()
p = j.GetJacketByPolicyID(3000050)

For some reason it's flagging the 2nd line in the GetJacketByPolicyID function saying the specified cast is not valid. So I'm guessing it's something I'm doing wrong. I'm sure the tblPolicy/tblPolicies class works right since I can create a new instance of a tblPolicy and set a few variables by hand and return it, so that's not it. I've also checked the datarow I'm fetching and there's no null values in the record, so that shouldn't be it either.

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved