score:2
Accepted answer
it sounds like you need to parameterize the property name in datacontext
. one way to do it is to pass in a function that takes a datacontext
and returns your enumerable (a func<datacontext, ienumerable<tsource>>
) and then you would pass in a lambda like dc => dc.document
as that parameter.
another option, which would work (but without type-safety) is to pass in the property's name as a string and then use reflection to access it.
public void processimportobjects<tsource, tdestination>
(func<datacontext, ienumerable<tsource>> dcproperty,
action<tdestination, importdatasource, int> processmethod,
importdatasource importsource,
int resultid)
{
using (datacontext dc = new datacontext())
{
dc.objecttrackingenabled = false;
mapper.createmap<tsource, tdestination>();
foreach (tsource d in dcproperty(dc))
{
tdestination doc = mapper.map<tsource, tdestination>(d);
processmethod(doc, importsource, resultid);
}
}
}
Source: stackoverflow.com
Related Query
- Converting a C# method to use generics referencing a property inside
- Why can't I use a range value's property as a parameter when calling a method in a LINQ query?
- How to use ToString() method to convert an integer to string inside LINQ
- How to use Extension method inside a LINQ statement?
- Use generics to make a method work for any data type
- How can i use the select method for Multiple properties with same property name
- LINQ to Entities query with join inside method for use in MVC app
- How can I use LINQ to select a property inside of an IEnumerable that's inside another IEnumerable?
- Use nested generics without a lot of code
- int array cannot use length property inside linq expression
- Select which property to use based on the value passed into the method
- Can I use get/set method inside a incoming new object generated by Linq?
- Use method for child property in entity framework query
- Use method return value inside linq where clause
- Pass property to method for use in LINQ query
- The Property method can only be used with primitive or complex properties. Use the Reference or Collection method
- How to use one Column of Sql query into another sql query inside C# Code
- Use multiple left joins to set DTO property inside select new linq query
- How to use LINQ to select object with minimum or maximum property value
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- What is the use of Enumerable.Zip extension method in Linq?
- C# - code to order by a property using the property name as a string
- Calling a method inside a Linq query
- How can I use continue statement in .ForEach() method
- How do I use the AsQueryable method asynchronously with MongoDb C# Driver 2.1?
- Using a partial class property inside LINQ statement
- Can I use a TryParse inside Linq Comparable?
- When to use an extension method with lambda over LINQtoObjects to filter a collection?
- Syntax to execute code block inside Linq query?
- How to use .ToDictionary() extension method on DataRow
More Query from same tag
- Querying a DataTable filtering for first char in field
- Custom Projection Operator using LINQ
- Linq Using Contains On An Array
- A generic filter method for Linq-EF queries
- How can I extract all Unique / Distinct Rows from a Datatable and save these rows in a new Datatable with same Columns?
- How to convert an array to a dictionary using LINQ
- Searching for records in several tables using EF TPT
- How to convert hierarchical list to flat list?
- Cannot query XML document using XDocument and get desired results
- Is it better to use Enumerable.Empty<T>() as opposed to new List<T>() to initialize an IEnumerable<T>?
- Why does C# not allow anonymous cast to objects?
- LINQ Query to insert data into the database
- Left join multiple IList<> using Linq
- Translating from SQL to Linq when having group by and order by
- LINQ OrderBy - passing parameter
- How To Join Tables from Two Different Contexts with LINQ2SQL?
- How to merge 2 XML files with C#
- date difference in EF4
- Making thing more succint using linq to entities for inner joins
- Flatten xml with text and element nodes using LINQ to XML
- Select query with subquery that can return List<string>
- Remove the Parent Node based on Child condition
- LINQ Query IList<string> to JSon Format
- Grab a List item based on newest datetime
- C# notation understanding Select(int.Parse)
- how to make Value cannot be null, null
- Grouping Query Linq
- How can I split a List<T> into two lists, one containing all duplicate values and the other containing the remainder?
- How I can order by descending and then take some items using linq?
- Impact of using AsParallel() and AsSequential() in the same query? C#