Click here to Skip to main content
12,065,678 members (45,475 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# SQL SQL-Server LINQ , +
I try it by using datatable like that

for (int i = 0; i < dtp.Rows.Count; i++)
            {
                if (dtp.Rows[i]["UserId"].ToString() == formid.ToString())
              {
                   
              }
           }

Now I need it but by using LINQ How can I loop linq query

my linq query like that

int ID = (from s in con.TUsers
                                    where s.UserId.Equals(comboBox1.SelectedValue)
                                    select s.UserId).SingleOrDefault();

help me please
thanx in advance.
Posted 25-Jun-12 19:10pm
Comments
Aman4.net 26-Jun-12 0:18am
   
Please do explain your requirements and problems/errors in question. Still here is a clue based on your question:
In string comparison with C#, we need to check that values have same character casing and no extra spaces etc.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

hi,

I LINQ no need to use loop it goes through all the records available in collection and check for the given where condition and returns the matching data.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  var UserID = (from row in dtp.AsEnumerable()
 where row.Field<int>("UserID")==comboBox1.SelectedValue
select row.Field<int>("UserID")).FirstOrDefault();
</int>

Any error about AsEnumerable, just add this assembly:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Data.DataSetExtensions.dll
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi,
No need to loop if you are using a LINQ

int SelectedID = int.Parse(Combobox.SelectedValue);
 
int UserID = 0;
try{
 
UserID = (from row in dtp.AsEnumerable() where row.Field<datatype>("UserID")== SelectedID select row.field<datatype>("UserID").FirstorDefault();</datatype>

Thanks
www.alacraft.com.au
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160207.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100