locked
C#.net LINQ query RRS feed

  • Question

  • User-2103114970 posted

    Hi ,

    Could anybody help on below scenario .

    I have a table

    RowID     EmpName                                        Remarks                         DOB   

    1              Murali,Krishna, KL*                         KL,KK,HK,PP

    2              K**                                                         KL,TT                     10/10/2015

    3                                                                                                         10/10/2015

     

     

     

    1. User will pass  exact employ name , example if  EMPName  is “Murali” or “KLT” ( this is KL* means any value start with “KL” max is 3 chars  and Remarks  “KK” AND DOB is empty  then first row has to display
    2. If User Enter “KAT” OR “KET”  as Employee name and Remarks “ TT” and DOB =”10/10/2015” then second row has to display.
    3. If user enter only  DOB value     then  row 3 has to display.

    Could anybody suggest me , how to write Linq  statement in C#.net  .

     

    Please let me know if anybody not understands.

     

    Thanks,

    Murali.

    Tuesday, October 27, 2015 5:27 PM

Answers

  • User614698185 posted

    Hi Murali,

    Based on your requirement, I created the sample for you, code shown below:

    using (var context = new TestDBEntities())
                {
                    var results = from x in context.EmpInfoes
                                  where
                                  ( //For Scenario1
                                    (x.EmpName == ("Murali") || (x.EmpName.StartsWith("KL") && x.EmpName.Length <= 3))
                                    && x.Remarks == "KK"
                                    && x.DOB == null
                                  )
                                  ||
                                  ( //For Scenario2
                                    (x.EmpName == "KAT" || x.EmpName == "KET")
                                    && x.Remarks == "TT"
                                    && x.DOB == new DateTime(2015, 10, 10)
                                  )
                                  ||
                                  ( //For Scenario3
                                    x.DOB != null
                                    && x.EmpName == null
                                    && x.Remarks == null
                                    && x.Other1 == null
                                    && x.Other2 == null
                                  //Other Fields euqals null
                                  )
    
                                  select new { RowID = x.RowID, EmpName = x.EmpName, Remarks = x.Remarks, DOB = x.DOB };
    
                    foreach (var result in results)
                    {
                        Console.Write(result.RowID
                            + "|" + result.EmpName
                            + "|" + result.Remarks
                            + "|" + result.DOB
                            + Environment.NewLine
                            );
                    }
                }
    

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 2:25 AM

All replies

  • User614698185 posted

    Hi Murali,

    how to write Linq  statement in C#.net  

    Why do you want to use Linq statement? You table only has three records. When user enter their information, why not use if...else... statement in C# directly?

    Best Regards,

    Candice Zhou

    Wednesday, October 28, 2015 5:59 AM
  • User-2103114970 posted

    Hi Candice,

    I have 3000 rows in my table with 40 columns, i just sent example how to write a query for above scenario,need a Linq statement.

    Could any body help me please.

    Thanks,

    Murali.

    Wednesday, October 28, 2015 10:33 AM
  • User614698185 posted

    Hi Murali,

    Based on your requirement, I created the sample for you, code shown below:

    using (var context = new TestDBEntities())
                {
                    var results = from x in context.EmpInfoes
                                  where
                                  ( //For Scenario1
                                    (x.EmpName == ("Murali") || (x.EmpName.StartsWith("KL") && x.EmpName.Length <= 3))
                                    && x.Remarks == "KK"
                                    && x.DOB == null
                                  )
                                  ||
                                  ( //For Scenario2
                                    (x.EmpName == "KAT" || x.EmpName == "KET")
                                    && x.Remarks == "TT"
                                    && x.DOB == new DateTime(2015, 10, 10)
                                  )
                                  ||
                                  ( //For Scenario3
                                    x.DOB != null
                                    && x.EmpName == null
                                    && x.Remarks == null
                                    && x.Other1 == null
                                    && x.Other2 == null
                                  //Other Fields euqals null
                                  )
    
                                  select new { RowID = x.RowID, EmpName = x.EmpName, Remarks = x.Remarks, DOB = x.DOB };
    
                    foreach (var result in results)
                    {
                        Console.Write(result.RowID
                            + "|" + result.EmpName
                            + "|" + result.Remarks
                            + "|" + result.DOB
                            + Environment.NewLine
                            );
                    }
                }
    

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 2:25 AM