如何返回此字符串数组值 [英] How do I return this string array values

查看:79
本文介绍了如何返回此字符串数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法 1 

public void UnReserveSelectedDevices(List< int> deviceIds, string userName)
{
string [] reservebyNames = new string [ 0 ];
reservebyNames = GerReserveby(deviceIds,userName, false );
}


方法 2

public string [] GerReserveby(List< int> ids, string userName,< span class =code-keyword> bool reserveOn)
{
使用(NamesTestDbContext db = new NamesTestDbContext())
{
foreach int id in ids)
{
FleetNames device = db.FleetNames.Find(id);
if (!device.ReservedBy.Equals(userName)& amp;& amp;!reserveOn)
{
< span class =code-keyword> string [] reserveby = new [] {device.ReservedBy}; // 返回new [] {m​​yString};
}
}
}

< b> 返回; < / b > ??????

}





任何人都可以告诉我,我如何退回'储备'(方法:2)数组值?并将其存储到'reservebyNames'中(方法:1)

解决方案

而不是 string  [],使用List< string>   method2; .. < /   string  >  





  public  List< string> GerReserveby(List< int> ids, string  userName, bool  reserveOn)
{
List< string> liRes = new List< string>();

使用(NamesTestDbContext db = new NamesTestDbContext())
{
foreach int id in ids)
{
FleetNames device = db.FleetNames.Find(id);
if (!device.ReservedBy.Equals(userName))
{
liRes.Add(device.ReservedBy.ToString() );
}
}
}

return liRes;

}



重复检查..

使用此..

< pre lang =c#> if (!liRes.Contains(device.ReservedBy.ToString()))
{
liRes.Add( device.ReservedBy.ToString());
}


你好,



试试这样:

  public   string  [] GerReserveby(List< int> ids , string  userName, bool  reserveOn)
{
string [] reservedBy = null ;
使用 var db = new NamesTestDbContext())
{
reservedBy = db.FleetNames.Where(x = > ids.Contains( x.Id )&&(!x.ReservedBy.Equals(userName)&&!reserveOn))。选择(x = > x.ReservedBy).Distinct()。ToArray();
}

return reservedBy;
}





在您的方法中,您将迭代所有 ID 并在每个循环中进行查询通过。获取所有数据需要更长的时间,特别是当您可以使用一个查询获取所有数据时。



我不知道您的数据库结构,但我认为这样做得到你的意思:)



你可以在CodeProject找到很多关于CRUD操作的教程。



[更新 - 评论]

要检索不带重复项的reservedBy名称,请使用Distinct方法。代码更新。



祝你好运!


Method 1:

public void UnReserveSelectedDevices(List<int> deviceIds, string userName)
        {                 
            string[] reservebyNames = new string[0];
            reservebyNames = GerReserveby(deviceIds, userName, false);              
        }


Method 2:

public string[] GerReserveby(List<int> ids, string userName, bool reserveOn)
        {
            using (NamesTestDbContext db = new NamesTestDbContext())
            {
                foreach (int id in ids)
                {
                    FleetNames device = db.FleetNames.Find(id);
                    if (!device.ReservedBy.Equals(userName) &amp;&amp; !reserveOn)
                    {
                        string[] reserveby = new[] {device.ReservedBy};   //return new[] { myString };
                    }
                }
            }

           <b> return ;</b>   ??????

        }



Could any one please let me know, How do I return the 'reservby' (Method:2) array values? and stores it to 'reservebyNames' from (Method:1)

解决方案

instead of string[], use List<string> in method2;..</string>



public List<string> GerReserveby(List<int> ids, string userName, bool reserveOn)
        {
          List<string> liRes=new List<string>();

            using (NamesTestDbContext db = new NamesTestDbContext())
            {
                foreach (int id in ids)
                {
                    FleetNames device = db.FleetNames.Find(id);
                    if (!device.ReservedBy.Equals(userName))
                    {
                      liRes.Add(device.ReservedBy.ToString());
                    }
                }
            }

          return liRes;

        }


For Duplicate check..
use this..

if(!liRes.Contains(device.ReservedBy.ToString()))
  {
     liRes.Add(device.ReservedBy.ToString());
  }


Hello,

Try like this:

public string[] GerReserveby(List<int> ids, string userName, bool reserveOn)
{
    string[] reservedBy = null;
    using (var db = new NamesTestDbContext())
    {
        reservedBy = db.FleetNames.Where(x => ids.Contains(x.Id) && (!x.ReservedBy.Equals(userName) && !reserveOn)).Select(x => x.ReservedBy).Distinct().ToArray();
    }

    return reservedBy;
}



In your method you're iteraing all ids and making query in every loop pass. It will take much longer time to get all data especially when you can get all data with one query.

I don't know your DB structure but I think that will get you the point :)

You can find many tutorial on CRUD operations here on CodeProject.

[Update - For comment]
To retrieve reservedBy names without duplicates use Distinct method. Code updated.

Good luck!


这篇关于如何返回此字符串数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆