score:3
Just populate a regular DataTable and you can use it from LINQ
using System;
using System.Data;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Project Name", typeof(string));
dt.Columns.Add("Project Date", typeof(DateTime));
for (int i = 0; i < 10; i++)
{
var row = dt.NewRow();
row.ItemArray = new object[] { i, "Title-" + i.ToString(), DateTime.Now.AddDays(i * -1) };
dt.Rows.Add(row);
}
var pp = from p in dt.AsEnumerable()
where (int)p["Id"] > 2
select p;
foreach (var row in pp)
{
Console.WriteLine(row["Id"] + "\t" + row["Project Name"] + "\t" + row["Project Date"]);
}
Console.ReadLine();
}
}
}
In the code above you can see how to use LINQ to filter out certain records from the DataTable. You'll need to add the System.Data.DataSetExtensions assembly to your references
IEnumerables are readonly so you can't really do it the way you're asking, but what I've presented is the correct way.
score:0
Maybe you misunderstood something. LINQ is a query language with no side-effects. Thus you can't fill up any structure with it. You can only create a query against any existing information and do some kind of transformation or filtering.
Afterwards you can use this query to fill up some element by using foreach()
, ToList()
or ToArray()
, but you cannot directly use LINQ to insert any element into some other object or collection.
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- MVC - Linq - Populate List<T> with Records in Another Table
- Populate a LINQ table in code?
- Make access possible to dynamic table LINQ EF6 Code First
- source code for LINQ 101 samples
- How to reinsert data from one table onto itself using LINQ in code migration?
- How to join one row to every row in source table using LINQ
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- Complex SQL to LINQ to populate Grid (Count items year by month) (MVC, EF Code First)
- c# Linq or code to extract groups from a single list of source data
- Linq to query matching column from a table and populate in another table
- pass decimal column value to populate results from a table using linq query
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Distinct in Linq based on only one field of the table
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- populate a dictionary using linq
- Populate a list with a specific range of numbers by using LINQ
- Populate List<string> with the same value with LINQ
- Casting to a derived type in a LINQ to Entities query with Table Per Hierarchy inheritance
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- LINQ to SQL Every Nth Row From Table
- Syntax to execute code block inside Linq query?
- How to add field not mapped to table in Linq to Sql
- Linq to Entity Join table with multiple OR conditions
- Linq to select data from one table not in other table
- Get X random elements from table in database using Linq or lambda in C#
- LINQ TO DataSet: Multiple group by on a data table
- How to populate a Class with Dictionary using LINQ 2
- Yield return from an indexed iteration via LINQ
- Add incremented property to IEnumerable by group using LINQ
- MongoDB GroupBy Aggregate and Count Documents C#
- Order list by value with maximum sequence appearance
- How to compare two c# lists data equal using linq
- Is NHibernate.Linq 1.0 GA Provider Production Ready
- How to perform linq query in JsonArray from Facebook C# SDK?
- LINQ expression duplicates values on select instead of cycling them
- Cross table VB.NET & SQL Server & Linq
- LINQ to SQL Expression "Orderby" help
- Control how EF materializes my data
- linq refactoring (possibly using nested group by clause)
- Implicit convert List<int?> to List<int>
- Unique List, but keep blank lines
- Entity Framework/ Linq - groupby and having clause
- Am I doing something wrong from a connection pooling point of view?
- Linq query parent child grouping issue
- System.Linq.Dynamic - Can I use IN clause in WHERE statement
- List.Where passing in object of different type (Predicates)