C# - Insert An IEnumerable<T> Collection With Dapper-dot-net?
Jun 17, 2011
how to insert records with dapper-dot-net. However, the answers, while informative, didn't seem to point me in the right direction. Here is the situation: moving data from SqlServer to MySql. Reading the records into an IEnumerable<WTUser> is easy, but I am just not getting something on the insert. First, the 'moving records code':
// moving data
Dim session As New Session(DataProvider.MSSql, "server", _
"database")
Dim resources As List(Of WTUser) = session.QueryReader(Of WTUser)("select * from tbl_resource")
This is weird. I have a class that inherits from IEnumrable whose Count property is reporting 0 (zero) elements but the For Each loop steps into the loop body and tries to use the variable where it should just be moving on. My code:
On Error Resume Next Dim d As Foo For Each d In fooCollection ' use d and throws an exception Next d
Weirder still, every time d is accessed i get an exception thrown in the output window:
A first chance exception of type 'System.NullReferenceException'
but i'm not stopping on the exception (not in a try/catch block).Is "On Error Resume Next" causing this weirdness?
Weirdness found:Per Rowland's and JohnH's comments i checked the Foo class:The GetEnumerator method inside of Foo didn't actually return anything! It had an empty body. That coupled with the On Error Resume Next before the loop caused the havoc! Wow this was ugly.
This is really a continuation from my earlier post. I've never worked with this datatype/collection (system.collection.generic.Ienumerable) before so I'm struggling to declare the query globally so I can reference the results within other sub routines/functions. I also am struggling to convert it to other types.how I can convert the collection of xml elements/system.collection.generic.Ienumerable into an xmltextreader (I know I will have to change the query below as it select the value of the node and not the node itself) I imagine by converting the query result to system.IO.stream My first port of call was MSDN but I find the material very difficult to understand for this particular subject.
I have an object called Result<T> that has a constructor which accepts an argument of IEnumerable<T>. I'd like to be able to pass in a datatable if possible.
I tried datatable.AsEnumerable(), but where I bind to the data it was complaining that 'MyProperty' is not a field or property on type 'DataRow' - which makes sense since 'MyProperty' isn't a property on 'DataRow', but it was a column in my datatable.Is there a way to convert a datatable to something that I can pass into the Result object and still have it bind to, say, a gridview?
I tried using the linq Zip method on IEnumerable but it does not work for more than 2 arrays.Here is an example in Python of what I am trying to do(I got p - nested IEnumerable - and need q - another nested IEnumerable):
I am giving the Dapper ORM a try. I am able to query data from a table using the code below:
Dim comments As List(Of Comment) Using conn = New SqlConnection(ConnectionString) conn.Open()
[Code]....
how to do paging/sorting using Dapper. EF has "skip" and "take" to help with this. I understand that a micro ORM does not have this built in but would like to know the best way to accomplish this.
When ever I try to update a record from data grid using a dataAdapter record it always showing ' update requires a valid insert command when passed data row collection with new rows' From specific sql server Table only. Resulting me holding up a project.
Is it good practice to store sub-collections of item X inside a parent collection of item X for caching 'filter-queries' which are performed on the parent collection? (They won't be used together (as in color AND type).) Or is it also ok to just Loop over the collection and return the correct entities in a temporary collection?
I see a couple of previously answered questions about adding an item to an IEnumerable in C#, but I'm stuck when trying to implement the proposed solution in VB.NET.
Option Strict On Dim customers as IEnumerable(Of Customer) ' Return customers from a LINQ query (not shown) customers = customers.Concat(New Customer with {.Name = "John Smith"})
The above code gives the error:
Option Strict On disallows implicit conversions from Customer to IEnumerable(Of Customer)
IEnumerable interface provides a instance method GetEnumerator that returns IEnumerator type opject .That's okey .But its very hezy to me that how IEnumerable objects are able to work with For Each loop ?When using For Each GetEnumerator method is not called directly from my code .Then from where and how GetEnumerator is called ?Basically my question is that when a class implements IEnumerabel interface then how a distinct behaviour is attached to that class?How it is used with For Each without calling GetEnumerator ?Here is the sample code that i am using :
Public Class Person Public firstName As String Public lastName As String Public Sub New(ByVal firstName As String, ByVal lastName As String)
I have been researching for a couple of days now and to no avail. Does Anyone know how to make a custom collection that take advantage of the collection editor? I would like to be able to have 3 Color Values, 1 Boolean and 1 String.
i'm using Enumerable.Except to check if a DataTable in memory is in sync with the table in database.
The background is: this DataTable and other frequently used tables are stored in the Cache of a WebApplication. But meanwhile i'm convinced that this is not a good approach because it's a source for nasty errors that are difficult to reproduce/debug.
Therefore i've created a function that checks if database and memory are in sync, otherwise an error-log will be created. This works perfectly. If there is a row in memory that is not in database, this row will be shown below "Difference in database", the same applies in reverse. But if rows exist in both datasources(the PK idRMA) and some values differ, the log will contain this row in two versions(below "Difference in database" and "Difference in database"). It is not easy to see the differences on the first sight.
Q: Is it possible to select only the properties that caused Except to think that first sequence is not in second?
This is the the complete function(the first lines are relevant):
Public Shared Sub CheckRmaMemoryInSyncWithDB() Dim inSyncText As String Dim color As Drawing.Color
when you use the object browser to see the features of a sortedlist elementat is not shown , but because a sorted list inherets features of ienumerable it works, cant that be corrected
While learning to use LINQ in VB.NET, I came across the following:Dim x As IEnumerable = (some LINQ query)
If you can't instantiate an interface, but only a concrete implementation of it, why is this allowed? Is there some difference between doing Dim x as (Type) and Dim x as New (Type)?
I have an IEnumerable(Of System.Data.DataRowView) and would like to set it to be the datasource of a datagridview - however when I try I get no results my IEnumerable variable in the eg below is called xIEnum
DataGridView1.DataSource = xIEnum
However when I go
DataGridView1.DataSource = xIEnum.ToList
it works.... However I want the data to automatically update when I change the underlying datasource that the IEnum is mapped to?
I have the follow Linq query that is in a web application that was converted from .NET 1.1 to 3.5:
dim objListOfFilteredDataRows = from datarows as datarow in objDataSet.tables(0).rows _ where datarows("SomeColumn") = SomeValue
I have the exact same query in an application that was created using .NET 3.5 and the query returns an IEnumerable. However the query in the converted application is returning:
**Edit: When I highlight the expression, the intellisense says that it doesn't know the type of objListOfFilteredDataRows and assumes its a type of "Object". Why is the type not infered in the converted 1.1 application but is infered in the "native" 3.5?**
What am I missing here? How do I convert the "WhereEnumeratorIterator`1 to an IEnumerable?
I've searched everywhere for some example code for this, but I can't figure out how to bind data returned from a function that looks like this to a gridview or dataset.
Public Function GetCompanyList() As IEnumerable(Of BusinessUser) Return (From companyList In CorporateLists Select companyList.User).Distinct()
Suppose I have an IEnumerable such as a List(TValue) and I want to keep track of whether this list is being accessed (to prevent issues with, say, adding to the list while it is being iterated over on a different thread); I can always write code such as the following:
Dim List1 As New List(Of Integer) Dim IteratingList1 As Boolean = False ' ... some code ... '
[Code]....
(I realize this code is very arbitrary, but it illustrates what I'm talking about.)
My question is whether there's a better/cleaner way to perform this check than by manually updating and accessing a Boolean, as above. I feel like there must be, but, to my knowledge, there isn't any IEnumerable class with a built-in "I am being iterated over" method or property. And writing a new class that implements IEnumerable and contains such a property seems like overkill to me.
In VB.Net you can easily get the text value of the first child element of an XElement like so:Dim sChildValue = xeParent.<MyChild>.Value()Of course that syntax is not supported in C#, but it produces the same IEnumerable(Of XElement) result as the Elements() method. So we can rewrite the above like so:
Dim sChildValue = xeParent.Elements("MyChild").Value()This Value() extension method is handy because often you are working with small XML documents and you just want the first node that matches. (One thing that annoys me about this method is that it seems to return Nothing/null if the IEnumerable list is empty due to the specified element not being found. To work around this I have created my own ValueOrBlank() method that returns an empty string in that situation instead.)
My problem is that I can't do the same thing in C#:var sChildValue = xeParent.Elements("MyChild").Value(); // won't compile, can't find Value() method
I checked my references/imports and they match the VB.Net project where the same call works. Fortunately I am able to use my custom ValueOrBlank() extension method to accomplish the same thing. But I'm curious as to what's missing in my C# project. I tried right-clicking on the Value() method call in Visual Studio and clicking "Go To Definition" to see if the Object Browser could tell me where the extension method is kept, but it just shows the System.String class. Does that mean this is one of those sneaky VB.Net-only features that the compiler itself supports, like the XML Axis < MyChild > syntax?
The following line is giving me an InvalidCastException when trying to convert to IEnumerable type, even though I have implemented IEnumerable interface. Return New CountryInfo(CountryCodes(i), CountryNames(i)) (also in bold below)
Imports System Imports Gaia.WebWidgets.HtmlFormatting Imports System.Collections.Generic Imports System.Globalization
If I have a List(Of x) and a List(Of y) is it possible to iterate over both at the same time?
Something like
for each _x as X, _y as Y in List(of x), List(of y) if _x.item = _y.item then 'do something end if next
These lists may be of differing sizes. I am using .Net2.0 which I suspect is my downfall here as I have a feeling LINQ would solve something like easily by joining the lists on there common id.
I'm trying to support Basic.NET on my framework so I'm trying to convert C# 4 code to Basic.NET 10. Microsoft is committed to "co-evolve" these two but I'm having a problem with collection initialization... I found that I can initialize a collection much like in C#:
I've got the following ADO.NET Entity Framework Entity Data Model:I want to find all the Policyholders with both a Service of a given Id and also a Keyword of a given Status.
This LINQ Does Not Work:
Dim ServicesId As Integer = ... Dim KeywordStatus As Integer = ... Dim FoundPolicyholders = From p As Policyholder In db.PolicyholderSet.Include("Keywords").Include("Services") _ Where p.Services.Id = ServicesId _ And p.Keywords.Status = KeywordStatus _ Select p
The Where clause cannot search the p.Services and p.Keywords EntityCollections in that way.