使用LINQ进行SQL查询

ven*_*kat -2 c# linq asp.net .net-3.5

请以LINQ格式帮助SQL Query - C#.NET 3.5

select distinct location, country from Customer where   Customer_Code ='1001';
Run Code Online (Sandbox Code Playgroud)

这是Query没有问题.在SQL中正常工作.但是当我在LINQ格式的ASP.NET代码中使用下面的LinQ查询时出错.请帮助一样.

编辑:

在ASP.NET代码中,

    var Query2 =  ds.Tables[0].AsEnumerable()
                 .Where(p => p.Field<string>("Customer_Code") == "1001")
                 .Select(p => new {p.Field<string>("Location"),p.Field<string>("Country")});
Run Code Online (Sandbox Code Playgroud)

错误:

错误:无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问声明匿名类型成员.

请帮助!!!

Ani*_*Ani 6

就像是:

var query = dbContext.Customer
                     .Where(customer => customer.Customer_Code == "1001")
                     .Select(customer => new { customer.Location, customer.Country })
                     .Distinct();
Run Code Online (Sandbox Code Playgroud)

这里的关键是使用匿名类型作为保存客户位置和国家的元组.


编辑:编辑后,看起来你正在使用数据表,而不是LINQ to SQL/Entities.在这种情况下,您可能需要以下内容:

var query = table.AsEnumerable()
                 .Where(p => p.Field<string>("Customer_Code") == "1001")
                 .Select(p => new 
                               {
                                  Location = p.Field<string>("Location"),
                                  Country = p.Field<string>("Country")
                               })
                 .Distinct();
Run Code Online (Sandbox Code Playgroud)