即使有更多项目,LINQ select也只选择1项

时间:2012-08-08 08:45:03

标签: c# sql linq

我有一个Linq语句,必须检查哪个客户端连接到哪个用户

     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);
        }
    }

但即使DB中有87个具有相同的userID,它也只选择一个。我一直在盯着自己死去。有人可以帮忙吗

3 个答案:

答案 0 :(得分:1)

我在这里尝试过,你的代码可以运行:

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为您输出结果。

答案 1 :(得分:0)

    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);
        }
    }

答案 2 :(得分:-2)

试试这个:

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;

也许有用。