开发者

IF else statement in Linq

开发者 https://www.devze.com 2023-03-09 15:43 出处:网络
I have a datatable which contains following data as SrNo RollName 11XYZ 9945ABC 150120POQ 109RTY I want to find out the rollnumber & Name for srno range by 0-100 & 100-200

I have a datatable which contains following data as

SrNo Roll  Name
1     1     XYZ
99    45    ABC
150   120   POQ
10    9     RTY

I want to find out the rollnumber & Name for srno range by 0-100 & 100-200

So the output will be

// For the range 0-开发者_StackOverflow100
SrNo Roll  Name
1     1     XYZ
99    45    ABC
10    9     RTY

// & For 100-200
SrNo Roll  Name
150   120   POQ

I am trying to implement through LINQ with if-else statement.

If anything else please suggest with LINQ !!


Code:

var items = 
    new[]
    {
        new { SrNo = 1, Roll = 1, Name = "XYZ" },
        new { SrNo = 99, Roll = 45, Name = "ABC" },
        new { SrNo = 150, Roll = 120, Name = "POQ" },
        new { SrNo = 10, Roll = 9, Name = "RTY" }
    };

const int NumberOfItemsInRange = 100;

var ranges = items
    .GroupBy(arg => arg.SrNo / NumberOfItemsInRange)
    .Select(arg => new { RangeNumber = arg.Key, Items = arg.ToList() })
    .ToList();

foreach (var range in ranges)
{
    Console.WriteLine("Range {0}-{1}", range.RangeNumber * NumberOfItemsInRange, (range.RangeNumber + 1) * NumberOfItemsInRange);
    foreach (var item in range.Items)
        Console.WriteLine("{0,10} {1,10} {2,10}", item.SrNo, item.SrNo, item.Name);
}

Output:

Range 0-100
         1          1        XYZ
        99         99        ABC
        10         10        RTY
Range 100-200
       150        150        POQ


Linq doesn't have if/else operator. For your case you need to use where clause as you need to filter the data

public IEnumerable<SrObj> GetSrObj (int min, int max)
{
    return from d in data_source
           where d.SrNo >= min && d.SrNo <= max
           select d;
}

//get 0-100
GetSrObj (0,100)
//get 100 -200
GetSrObj (100,200)
//Get both ranges
GetSrObj (0,100).Union(GetSrObj (100,200))


Your question reads to me like you want a grouping of data by 100s. If this is so, try the following code snippet:

// grouping by srno
var results = from row in table.AsEnumerable()
                let key = row.Field<int>("SrNo") / 100
                group row by key into rowgroups
                select rowgroups;

This will group your rows in batches by the SrNo field. Verification:

foreach (var rowgroup in results)
{
    Console.WriteLine("\nGroup {0}", rowgroup.Key);
    foreach (DataRow row in rowgroup)
    {
        Console.WriteLine("{0}\t{1}\t{2}", 
            row.Field<int>("SrNo"),
            row.Field<int>("Roll"),
            row.Field<string>("Name"));
    }
}


var dt = new DataTable();
dt.Columns.Add(new DataColumn("SrNo", typeof (int)));
dt.Columns.Add(new DataColumn("Roll", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));

dt.Rows.Add(1, 1, "XYZ");
dt.Rows.Add(99, 45, "ABC");
dt.Rows.Add(150, 120, "ROQ");
dt.Rows.Add(10, 9, "RTY");

var result1 = from r in dt.AsEnumerable()
              where r.Field<int>("Roll") >= 0 && r.Field<int>("Roll") <= 100
              select r;

var result2 = from r in dt.AsEnumerable()
              where r.Field<int>("Roll") >= 100 && r.Field<int>("Roll") <= 200
              select r;


var result = from p in table1 where p.srno<100&&p.srno>0 select p;

Feel free to change the numbers etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消