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 Query
- 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
More Query from same tag
- Compare two list Items using LINQ
- LINQ Error with GroupBy
- optimize linq query with related entities
- Remove item from List C#
- Can't extract value from expression of type: Parameter
- Get custom linq lambda for sorting
- Map Expression<Func<Dto, bool>> to Expression<Func<Entity, bool>> using AutoMapper
- LINQ for CRM how to use C# list in where clause
- Write to end of XML with LINQ to XML
- This function can only be invoked from LINQ to Entities. How can I solve this?
- How to Convert Expr<'a -> 'b> to Expression<Func<'a, obj>>
- Linq query - reading nested XML to dictionary
- Issue with LINQ inside XML Literals when generating XSD
- Entity Framework INNER JOIN with "BETWEEN" date range
- How to place an If/Else statement in a where clause in a linq query?
- How do I group by two fields and return the original objects that match?
- LINQ to Json data retrieval from polymorphic json
- How can I do a count in Linq with many to many
- How do I reference a field in Linq based on a dynamic fieldname
- Joining Dictionary key that matches class List's list item into a new list - C#
- Enumerable.Last() Throws InvalidOperationException "Sequence contains no matching element"
- LINQ Select New
- Find or Create Element in LINQ-to-XML
- C# Master Detail - Data Binding
- C#/Linq: Apply a mapping function to each element in an IEnumerable?
- LINQ Solution for the highest line with two or more same data
- LINQ unexpected behavior when returning IEnumerable and calling ToArray
- How to skip incorrect DateAndTime format when retrieving data from the database
- Is there a JS library that supports writing linq to sql queries with nodejs?
- DataView select expression column with char 'A' in string position 6