使用LINQ从表中仅获取一个值

使用LINQ从表中仅获取一个值,linq,select,subquery,Linq,Select,Subquery,我有一个客户表和一个地址表(客户id、地址名、地址姓氏、地址街、地址城市、地址邮编、, 地址(存储时间),其中客户id作为外键 一个客户可以有多个地址 现在,我正在尝试使用LINQ as bellow仅获取最后输入的地址,这将允许我将地址放入字符串并返回: 代码: var customerAddress = (from c in myDB.address where (c.customer_id == customerId)

我有一个客户表和一个地址表(客户id、地址名、地址姓氏、地址街、地址城市、地址邮编、, 地址(存储时间),其中客户id作为外键

一个客户可以有多个地址

现在,我正在尝试使用LINQ as bellow仅获取最后输入的地址,这将允许我将地址放入字符串并返回: 代码:

var customerAddress = (from c in myDB.address
                       where (c.customer_id == customerId)
                       select new
                       {
                           c.customer_id,
                           c.address_forename,
                           c.address_surname,
                           c.address_street,
                           c.address_city,
                           c.address_zip,
                           c.address_storedtime
                       }).GroupBy(g => new
                       {
                           Customer = .customer_id,
                           Address = g.address_forename + " " + g.address_surname + " " + g.address_street + " " +        g.address_city + " " + g.address_zip 
                       }).Select(g => new
                       {
                           g.Key.Customer,
                           g.Key.Address,   
                           StoredTime = g.Max(x => x.address_storedtime)
                       }).Disinct();/*First();*/

string result = "";

foreach (var ad in customerAddress)
{
    if (ad.Address != null)
    {
        result = ad.Address;
    }
    break;
}

return result;

我正在为客户的不同DB地址获取相同的地址字符串,而我正在尝试仅获取一个。

将您的
foreach
替换为

var result =
    customerAdress.Any(ad => ad.Address != null) 
    ? customerAdress.Last(ad => ad.Address != null).Address
    : default(string);

因为您已经在按客户id进行筛选,所以分组子句是不必要的。您应该能够为客户排序结果,并更简单地投影地址

var customerAddress = (from c in myDB.address
                        where (c.customer_id == customerId)
                        orderby c.address_storedtime descending
                        select c.address_forename + " " + c.address_surname + " " + c.address_street + " " +        c.address_city + " " + c.address_zip)
                      .FirstOrDefault();

感谢您的快速回答,但我得到了一个巨大的错误,我不知道:LINQ to Entities无法识别上次[f_uAnonymousType223[System.Int32,System.String,System.DateTime]的方法'f_uAnonymousType223](System.LINQ.IQueryable
1[f_uAnonymousType22
3[System.Int32,System.String,System.DateTime]],System.Linq.Expressions.Expression
1[System.Func
2[f_uAnonymousType22`3[System.Int32,System.String,System.DateTime],System.Boolean]])方法,此方法无法转换为存储表达式。我只想得到一次结果。但它(使用我的代码示例)向我显示了4次,因为addAddress表中有4个不同的地址:LINQ to Entities不识别方法'f_uanonymoustype22
3[System.Int32,System.String,System.DateTime]Last[f_uanonymoustype22
3](System.LINQ.IQueryable
1[f_uanonymoustype22
3[Syste‌​m、 Int32,System.String,System.DateTime]],System.Linq.Expressions.Expression
1[System.Func
2[f_uAnonymousType22`3[System。‌​Int32,System.String,System.DateTime],System.Boolean]]),并且此方法不能在代码的这一部分中转换为存储表达式:GroupBy(g=>new{Customer=.Customer\u id,Address=g.Address\u forename…不是“.Customer\u id”应该是“g.Customer\u id”吗?