Click here to Skip to main content
15,118,017 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a requirement like this:
ID   || Buss_Code || Transt_Code
1    ||   AB      ||    XY
1    ||   AB      ||    YZ
1    ||   AB      ||    ABC
2    ||   CD      ||    BAL
2    ||   CD      ||    BGH
2    ||   CD      ||    DFR

I want to validate,

for every ID=1 and Buss_Code = AB, there must be atleast one Transt_Code = XY.
similarly, for every ID=2 and Buss_Code = CD,
there must be atleast one Transt_Code = BAL.

I AM USING C# ASP.NET and also LINQ. Please answer regading to C# and linq.

Please help me on this. Its urgent.
Thanks in advance!
Posted
Updated 20-Nov-15 1:08am
v2
Comments
Tomas Takac 20-Nov-15 6:11am
   
I fail to see the connection to ASP.NET, SQL or LINQ. In what form do you have the data? Where/when do you want to validate it? What did you try so far?
Member 11508005 20-Nov-15 6:18am
   
Code is built in asp.net wpf c# code, Database is Oracle database
John C Rayan 20-Nov-15 7:25am
   
But you have mentioned that you use ASP.NET. Can you change your question to make it clearer.
John C Rayan 20-Nov-15 7:27am
   
When you say you want validation, can you explain bit more so we can help you. You mean you want to filter on the condition you mentioned ???

1 solution

Take a look at example:

C#
//create DataTable and example data
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Buss_Code", typeof(string)));
dt.Columns.Add(new DataColumn("Transt_Code", typeof(string)));
dt.Rows.Add(new Object[]{1, "AB", "XY"});
dt.Rows.Add(new Object[]{1, "AB", "YZ"});
dt.Rows.Add(new Object[]{1, "AB", "ABC"});
dt.Rows.Add(new Object[]{2, "CD", "BAL"});
dt.Rows.Add(new Object[]{2, "CD", "BGH"});
dt.Rows.Add(new Object[]{2, "CD", "DFR"});
dt.Rows.Add(new Object[]{3, "EF", "FRE"});

//create Dictionary 
Dictionary<int, Tuple<string, string>> validDic = new Dictionary<int, Tuple<string, string>>();
validDic.Add(1, Tuple.Create("AB","XY"));
validDic.Add(2, Tuple.Create("CD","BAL"));

var result = dt.AsEnumerable()
		.Select(r=> new
			{
				ID = r.Field<int>("ID"),
				BussCode = r.Field<string>("Buss_Code"),
				TranstCode = r.Field<string>("Transt_Code"),
                //please, read comment below the code
				IsValid = !validDic.ContainsKey(r.Field<int>("ID")) ? false :
							r.Field<string>("Buss_Code") == validDic[r.Field<int>("ID")].Item1 &&
								r.Field<string>("Transt_Code") == validDic[r.Field<int>("ID")].Item2
			});
//display result
foreach(var d in result)
{
	Console.WriteLine("{0} {1} {2} {3}", d.ID, d.BussCode, d.TranstCode, d.IsValid);
}


Above lambda expression is equal to:
pseudo-code:
if(condition) {resultIfTrue} else {resultIfFalse}
explanation:
if(dictionary does not contain key) {return false} else {return a result of comparison of Buss_Code and Transt_Code to items in Tuple}

Result:
ID BussCode TranstCode IsValid
1  AB        XY        True 
1  AB        YZ        False 
1  AB        ABC       False 
2  CD        BAL       True 
2  CD        BGH       False 
2  CD        DFR       False 
3  EF        FRE       False 


Try!

[EDIT]

Simplified version:
C#
var validData = dt.AsEnumerable()
		.Where(r=> !validDic.ContainsKey(r.Field<int>("ID")) ? false :
							r.Field<string>("Buss_Code") == validDic[r.Field<int>("ID")].Item1 &&
								r.Field<string>("Transt_Code") == validDic[r.Field<int>("ID")].Item2);


Returns:
ID Buss_Code Transt_Code
1  AB        XY 
2  CD        BAL 
   
v3
Comments
Liju Sankar 22-Nov-15 20:26pm
   
Maciej, by looking at the question, I think he want to perform a different validation. that means he wanted to check if ID 1 contains any TranstCode XY under bus code AB.

Your code helps him to validation each rows!
Maciej Los 23-Nov-15 9:08am
   
You may be right... I did understand that OP wants to check if there is - at least - one specific Transt_Code for each ID and Buss_Code.
I provided a way to achieve that, but there's at least few other options, such as aggregating data ;)

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