3

我收到错误

“无法将 'System.Data.Linq.DataQuery`1[StockManagement.Models.Client]' 类型的对象转换为 'StockManagement.Models.Client'。”

public class Client
        {
            public int ClientID { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public string Mobile { get; set; }
            public string Telephone { get; set; }
            public string Fax { get; set; }
            public string Company { get; set; }
        }


private StockDataClassesDataContext dc;
 public Client GetClient(int clientID)
        {
            dc = new StockDataClassesDataContext(ConString.DBConnection);
            Client query = (Client)(from tbclient in dc.tblClients
                                  where tbclient.ClientID == clientID
                                  select new Client
                                  {
                                      Address = tbclient.Address,
                                      ClientID = tbclient.ClientID,
                                      Company = tbclient.Company,
                                      Fax = tbclient.Fax,
                                      Mobile = tbclient.Mobile,
                                      Name = tbclient.Name,
                                      Telephone = tbclient.Telephone
                                  });
            return query;
        }
4

1 回答 1

9

您的查询返回一个IEnumerable<Client>, 您需要FirstOrDefault, First, SingleOrDefault.. 查看 MSDN 以找到最适合您的那个。

Client query = (from tbclient in dc.tblClients
                                  where tbclient.ClientID == clientID
                                  select new Client
                                  {
                                      Address = tbclient.Address,
                                      ClientID = tbclient.ClientID,
                                      Company = tbclient.Company,
                                      Fax = tbclient.Fax,
                                      Mobile = tbclient.Mobile,
                                      Name = tbclient.Name,
                                      Telephone = tbclient.Telephone
                                  }).FirstOrDefault()
于 2013-04-29T06:41:34.557 回答