LINQ选择只选择1,即使有更多的项目
我有了要检查的客户端连接一个LINQ报表项目,其用户LINQ选择只选择1,即使有更多的项目
public List<Client_Dto> GetClientByBehandelaar(string loggedInUserId)
{
try
{
int userID = Convert.ToInt32(loggedInUserId);
nestorDBDataContext db = new nestorDBDataContext();
var result =
(from relaties in db.tbl_Relaties
where relaties.ID_Persoon == userID
select new Client_Dto()
{
ID = relaties.NestorNrCliënt
}).ToList();
List<Client_Dto> clienten = result;
return clienten;
}
catch (Exception e)
{
throw new ArgumentException("GetClientByBehandelaar Failed " + e);
}
}
,但只选择一个,即使有87多个在具有相同用户标识的数据库。我一直盯着自己死在这一个..谁能帮
我在这里尝试过了,你的代码工作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Relatie
{
public Int32 ID_Persoon { get; set; }
public Int32 NestorNrCliënt { get; set; }
public Relatie(int ID_Persoon, int NestorNrCliënt)
{
this.ID_Persoon = ID_Persoon;
this.NestorNrCliënt = NestorNrCliënt;
}
}
class nestorDBDataContext
{
public List<Relatie> tbl_Relaties = new List<Relatie> {
new Relatie(15, 27),
new Relatie(15, 28),
new Relatie(15, 29),
new Relatie(15, 30),
new Relatie(14, 30),
new Relatie(14, 30),
new Relatie(14, 30),
};
}
class Program
{
public struct Client_Dto{
public Int32 ID;
}
public static List<Client_Dto> GetClientByBehandelaar(string loggedInUserId)
{
try
{
int userID = Convert.ToInt32(loggedInUserId);
nestorDBDataContext db = new nestorDBDataContext();
var result =
(from relaties in db.tbl_Relaties
where relaties.ID_Persoon == userID
select new Client_Dto()
{
ID = relaties.NestorNrCliënt
}).ToList();
List<Client_Dto> clienten = result;
return clienten;
}
catch (Exception e)
{
throw new ArgumentException("GetClientByBehandelaar Failed " + e);
}
}
static void Main(string[] args)
{
Console.WriteLine(GetClientByBehandelaar("15").Count());
Console.ReadKey();
}
}
}
也许你需要在这个布尔comarison ID_Persoon == userID
使用的Debug.WriteLine看输出结果为您服务。
你正在对'tbl_Relaties'做假设,这可能不是真的。你确定这是OP的数据结构和数据内容的样子吗? =) – 2012-08-08 09:31:54
绝对不是,我假设我不知道能够测试任何东西...... linq被测试,它的确定 - 错误是在代码的那一部分以外的地方:) – Johannes 2012-08-08 09:35:42
试试这个:
int userID = Convert.ToInt32(loggedInUserId);
nestorDBDataContext db = new nestorDBDataContext();
List<Client_Dto> clienten =
(from relaties in db.tbl_Relaties
where relaties.ID_Persoon.Equals(userID)
select relaties
).ToList();
return clienten;
也许它的工作。
public List<Client_Dto> GetClientByBehandelaar(string loggedInUserId)
{
try
{
int userID = Convert.ToInt32(loggedInUserId);
List<Client_Dto> result = new List<Client_Dto>();
using (nestorDBDataContext db = new nestorDBDataContext())
{
IEnumerable<Client_Dto> client_dto =
(from relaties in db.tbl_Relaties
where relaties.ID_Persoon == userID
select relaties);
result = client_dto.ToList();
return result;
}
}
catch (Exception e)
{
throw new ArgumentException("GetClientByBehandelaar Failed " + e);
}
}
这和OP已经做的有什么不同?除了你还创建了一个你甚至没有使用过的列表实例。虽然我会赞扬你实际上在使用'使用'。 – 2012-08-08 09:20:39
哪个列表没有使用哪个实例? – 2012-08-08 09:25:09
'List
生成的SQL看起来像什么?你的模式对于那个属性是什么样的? – 2012-08-08 08:48:44
@Nicolas你可能在查询错误的字段和/或表格吗?我想你可能会查询主表而不是外键表(因此为什么你只能得到一个单一的结果)。也不需要你做'列表 clienten = result;返回clienten;'只返回结果。 –
James
2012-08-08 08:50:19
另一个关于卫生的建议。当你完成时处理你的'db',或者把整个构造放入一个'using'范围声明中。 – 2012-08-08 09:22:44