1

I am new to LINQ queries, please help me out to find the solution.

I have a source in Entity Framework data model, there is a table currency bound to the source with columns currencyID and CurrencyName.

I need to get the values from the database to the DataTable using a LINQ query.

I tried something like mentioned below but it's not working:

 var dataset = Source.T_Currency
    .Where(x=> x.CurrencyID == x.CurrencyID &&  x.CurrencySymbol == x.CurrencySymbol)
    .Select(x => new x.Currency
    {
        CurrencyID = x.CurrencyID,
        CurrencySymbol = x.CurrencySymbol
    }).ToList();
0

2 Answers 2

1

If you want to select all rows from T_Currency then try

Source.T_Currency
    .Select(x => new
    {
        x.CurrencyID,
        x.CurrencySymbol
    })
    .ToList()

To filter result by any value add Where statement before Select:

Source.T_Currency
    .Where(x => x.CurrencySymbol == myCurrency) // where myCurrency is variable/parameter
    .Select(x => new
    {
        x.CurrencyID,
        x.CurrencySymbol
    })
    .ToList()

It is example with Select statement but actually in this case it is not requied, so Source.T_Currency.ToLost() returns the same result as the first code snippet. Difference is in type of values but if you can use original class then you should not create anonimous type.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I want to select all the rows from T_Currency into a data table.
` DataTable table = new DataTable(); using (var reader = ObjectReader.Create(t)) { table.Load(reader); }`
0

you should use LINQ Join like this example:

        var custSupJoin =
            from sup in suppliers
            join cust in customers on sup.Country equals cust.Country
            select new { Country = sup.Country, SupplierName = sup.SupplierName, CustomerName = cust.CompanyName };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.