Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LINQ help

Status
Not open for further replies.

vbdbcoder

Programmer
Nov 23, 2006
246
US
How do I write LINQ to query against a Dataset for the below query.

select [Column 0], [Column 9] from [TABLE_1]
where [Column 0] = 'NY'
group by [Column 0], [Column 9]

My table contains records like:
[Column 0], [Column 9]
NY aaa
NY aaa
NY bbb
WI ccc

The target is to get after running the query, but using LINQ:
[Column 0], [Column 9]
NY aaa
NY bbb


Thanks,
 
If you're not aggregating the data, are you using group by just to get a distinct set of values?

you could try something like this:

Code:
static void Main(string[] args)
{
    var results = (from d in GetData()
                    where d.Column0 == "NY"
                    select new { d.Column0, d.Column9}).Distinct();

    foreach (var item in results)
    {
        Console.WriteLine(string.Format("{0} - {1} ", item.Column0, item.Column9));
    }
    Console.ReadLine();

}

public class TABLE_1
{
    public string Column0 { get; set; }
    public string Column9 { get; set; }
}

public static List<TABLE_1> GetData()
{
    List<TABLE_1> lst = new List<TABLE_1>();
    lst.Add(new TABLE_1() { Column0 = "NY", Column9 = "aaa" });
    lst.Add(new TABLE_1() { Column0 = "NY", Column9 = "aaa" });
    lst.Add(new TABLE_1() { Column0 = "NY", Column9 = "bbb" });
    lst.Add(new TABLE_1() { Column0 = "WI", Column9 = "ccc" });
    return lst;
}

more on linq here: 101 Linq Samples

good luck!

-Mark


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top