Click here to Skip to main content
15,885,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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)
Posted
Updated 6-Dec-14 0:36am
v4
Comments
OriginalGriff 6-Dec-14 6:49am    
We can;t answer that - because we have no idea what your FleetNames class is all about, or what it contains. So we have no idea where the strings you want to return are coming from.
Use the "Improve question" widget to edit your question and provide better information.

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


C#
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..
C#
if(!liRes.Contains(device.ReservedBy.ToString()))
  {
     liRes.Add(device.ReservedBy.ToString());
  }
 
Share this answer
 
v2
Comments
vasanthkumarmk 6-Dec-14 8:14am    
Excellent!!!!!! Answer..... I got the issue in the string[]. When I decide to ask question in this page I had saw your post. Now using the List<string> my issue got resolved. Thanks a lot......... Thanks..... Thanks....
vasanthkumarmk 6-Dec-14 8:21am    
And also one question, I want to remove duplicate values in the list before return.
/\jmot 6-Dec-14 8:35am    
updated, for duplicate value entry..
check now.
Hello,

Try like this:
C#
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!
 
Share this answer
 
v4
Comments
vasanthkumarmk 6-Dec-14 6:59am    
Ya... I missed that. Thank you so much Marcin Kozub......
Marcin Kozub 6-Dec-14 7:01am    
You're welcome :)
/\jmot 6-Dec-14 7:03am    
+5, easy and simple one.
Marcin Kozub 6-Dec-14 7:51am    
Thx! :)
Maciej Los 6-Dec-14 7:06am    
Good job!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900