linq

How can I return all the columns of a table using multiple distinct columns in Linq

How can I return all the columns of a table using multiple distinct columns in linq. I need to return all columns of a table with multiple distincts? Thanks ...

Construct a Linq Expression from a generic method.

(sorry, I tried to format it well, but I cannot get the code formatting to work correctly) I get: Incorrect number of arguments supplied for call to method 'System.Linq.IQueryable`1[System.String] Take[String](System.Linq.IQueryable`1[System.String], Int32)' when I execute: string[] companies = { "Consolidated Messenger", "Alpine Sk...

C# Linq finding duplicate row

Person Name City Joe Houston Jerry London Alex Houston Jerry London How to return duplicate row using LINQ like Sql SELECT name, city, count(*) FROM collection GROUP BY name,city HAVING count(*) > 1 I tried something var qry = from m in context.Collections ...

Can LINQ do this for me?

Hi.. new with Linq and very excited with this feature. However, I want to see if I can do the following with LINQ: DataView source = (DataView) MyDataGrid.ItemsSource; foreach (DataRowView vw in source) { if (vw.Row[dummyColumnIndex] != null && vw.Row[dummyColumnIndex].ToString() == DisplayValue) { vw...

Linq expression tree string comparison

I am trying to construct an expression tree to operate against a string array. I need to figure out how to use the Equal method. Could anyone give me an example of 1) using the Expression.Equal (or .Equals) method to compare a string to a constant, and 2) using an Expression of whatever sort to use the string.Contains() method for filt...

Update list of records using linQ

Hello, I have a editable gridview in my aspx page. Users can enter records and after hitting submit using LinQ I am inserting the records to the database. It is working absolutely fine. Dim db as new empDBDataContext Dim rw As GridViewRow Dim emp as new employee emp.name="test" emp.city="NYC" emp.age=40 For each rw in GridView1.Rows Di...

How would I get the corresponding Key for the maximum Value in a Dictionary(Of SomeEnum, Integer) using LINQ?

I did a fair bit of searching for an answer to this question, but no example that I could find got all the way to where I need to be. I've got a Dictionary(Of SomeEnum, Integer) that gets filled up while looping through some objects that have a property with type SomeEnum. Once the loop is done, I want the SomeEnum type that occurs the ...

Getting the same element without changing it using Linq

I have a method like this: public List<Fruit> Traverse (IEnumerable<Fruit> collection, Action<Fruit> action) I can do this: Traverse (array, f => f.Text); How can I call the action so I get the same element? Traverse (array, f => f); C# compiler doesn't allow me to do this. EDIT: List<Fruit> result = ... foreach (Fruit fruit i...

conditional in new object assignment?

what is shorthand for this or should i not do this? do i call a method to check? bool assign=value; objectsList = from o in objects select new { id=o.id, name=o.name or "" (if assign==false), } ...

SQL to Linq Equivalent

How to convert the following SQL to LINQ select sum(salary),Deptid from person where deptid=1 or deptid=2 group by deptid ...

Is it better to use Enumerable<T>.Empty() as opposed to new List<T> to initialize an IEnumerable?

Suppose you have a class Person : public class Person { public string Name { get; set;} public IEnumerable<Role> Roles {get; set;} } I should obviously instanciate the Roles in the constructor. Now, I used to do it with a List like this : public Person() { Roles = new List<Role>(); } But I discovered this static method i...

Reusing part of the query with joins

I need to implement 3 public methods that all rely on the same initial join. Would a solution like the one described below work? The code below is an example, the real stuff is much more complicated, that's why I want to reuse it. private Tuple<Customer,Order> GetCustomerOrders() { from c in customers join o in orders o...

Linq, emulating joins, and the Include method

Hello, I'm looking into an issue that is related to... http://stackoverflow.com/questions/416847/join-and-include-in-entity-framework Basically the following query returns the list of "Property" objects the current user has permissions ( ACLs ) to view. IQueryable<Property> currPropList = from p in ve.Property ...

How much is too much data for and XML file, and what are some file based database alternatives?

I'm writing an application that keep track of a library of music, and I need a way to store the list of tracks, albums and other data. Usually for something like this I would use an XML file to save the data. And then I would use an ADO.NET DataTable to manipulate the data. But this program could potentially be saving a large number of d...

How to generate entities to database

I create a linq to sql class Create all entities that i need for my app Now i need to create this entities in the database is there a way to generate all of my entities? yes i want to generate tables for the entities ...

can't import xml to dataset if xmlnode has attribute?

Hi: I am using the following codes to read xml file to a datagridview in c#: newDataSet.ReadXml(filepath); dataGridView3.DataSource = newDataSet; dataGridView3.DataMember = "aaa"; and my xml file is like this: <root> <aaa> <Param_1>1</Param_1> <Param_1>2</Param_1> <Param_1>3</Param_1> </aaa> </root> I can...

C# -LINQ- Extension Method

What is the extension method equivalent of the following LINQ? var qry = from a in context.A_Collections from b in context.B_Collections where a.PK == b.PK select new { A_key = a.PK, A_Value = a.Value, B_Key = b.PK, B_value = b.value ...

Create XML subtree from string in LINQ?

I want to modify all the text nodes using some functions in C#. I want to insert another xml subtree created from some string. For example, I want to change this <root> this is a test </root> to <root> this is <subtree>another</subtree> test </root> I have this piece of code, but it inserts text node, I want to create xml subtree ...

Working with DefaultOrEmpty in LINQ

Incase of Default or Empty i want to supply some value string[] str = {string.Empty, "hello", "world" }; var select = str.Select(s => s).DefaultIfEmpty("nodata"); GridView1.DataSource = Select; GridView1.DataBind(); why did not my grid fill with nodata hello world instead i receive hello world ...

Linq difference between element ,element.value

What is the difference between select(a=>a) and select(a=>a.value) ? .Where do i need the second one? ...