# C# or LINQ - How to search matching record and display specified format.[Answered] RSS

## 11 replies

Last post Jul 27, 2017 08:44 AM by gani7787

• ### C# or LINQ - How to search matching record and display specified format.

Hi,

I done some sample code which should search matching record and display in specified format.

i am unable to get exact format.

my output is : 100,300 (because only matching ENGLISH H and MATHS L)

see my sample code's

```    protected void Button7_Click(object sender, EventArgs e)
{

string result = null;

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };

if (OUTFORMULA != null)
{
string re1 = null;
for (int i = 0; i < OUTFORMULA.Length; i++)
{
string re = null;
for (int j = 0; j < DIMVALUE.Length; j++)
{
string[] str = DIMVALUE[j].Split('=').ToArray();
if (OUTFORMULA[i] == str)
{
re1 = str;
break;
}

}

re = re + re1 + ",";

result = string.Concat(result, re); // OutPut Should come only 100,300

}

}

}```

• ### Re: C# or LINQ - How to search matching record and display specified format.

With LinQ:

```protected void Button7_Click(object sender, EventArgs e)
{
string result = null;
string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };
string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };
var varResult = DIMVALUE.Where(str => OUTFORMULA.Any(str1 => str.Contains(str1))).Select(str => str.Split('=')).ToArray();
result = string.Join(",", varResult);
}```

नमस्ते,
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
• ### Re: C# or LINQ - How to search matching record and display specified format.

```result = OUTFORMULA.Join(DIMVALUE.Select(d=>d.Split('=')),
o => o,
d => d,
(o, d) => d)
.Aggregate((res, t) => \$"{res}, {t}");```

(The kaushalparik... solution won't work if the name of one subject is a substring of another subject. For example, if you have a subject PHYSICS L and another PHYSICS LIG)

• ### Re: C# or LINQ - How to search matching record and display specified format.

Is it possible to add one more conditions. let's say

for example,

if "TAMIL TAG" will not found in the DIMVALUE then give label "No Data".

so the final output,

No Data,100,300,No Data,No Data

• ### Re: C# or LINQ - How to search matching record and display specified format.

#### PaulTheSmith

(The kaushalparik... solution won't work if the name of one subject is a substring of another subject. For example, if you have a subject PHYSICS L and another PHYSICS LIG)

Aha, I got that what you mean. I thought that was kind of requirement/issue with the question. Of course, the example I posted can break into PHYSICS L and PHYSICS LIG trap. Thanks for pointing that out. Correction here:

`protected void Button7_Click(object sender, EventArgs e){    string result = null;    string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };    string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };    var varResult = DIMVALUE.Where(str => OUTFORMULA.Any(str1 => (string.IsNullOrEmpty(str) ? ("0=0") : str).Split('=').Equals(str1))).Select(str => str.Split('=')).ToArray();    result = string.Join(",", varResult);}`

नमस्ते,
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
• ### Re: C# or LINQ - How to search matching record and display specified format.

I want to get the result which is mentioned in the OUTFORMULA.

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

Some times OUTFORMULA Value is missing in DIMVALUE. (For Ex : TAMIL TAG,"PHYSICS LIG", "SCIENCE GROUP")

string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };

So the user can understand if we mentioned "No data" for the particular value, it will be useful to refer the OUTFORMULA.

OUTPUT Result should idsplay in the order of OUTFORMULA.

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

Resutl = "No Data",100,300,"No Data","No Data"

• ### Re: C# or LINQ - How to search matching record and display specified format.

I want to get other values also

Result  = "No Data",100,300,"No Data","No Data"

The final intention is Result value count should always equal to OUTFORMULA Count

Result  = "No Data",100,300,"No Data","No Data" = ==> Count =5 and order by same

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" }; ==> Count = 5 and order by same

• ### Re: C# or LINQ - How to search matching record and display specified format.

Are you getting my points.

if not, pls. let me know...

• ### Re: C# or LINQ - How to search matching record and display specified format.

#### gani7787

Result  = "No Data",100,300,"No Data","No Data"

Please check out below example (however, there may be some better solution than this one):

```protected void Button7_Click(object sender, EventArgs e)
{
string result = null;
string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };
string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };
var varResult = OUTFORMULA.Select(str => DIMVALUE.Any(str1 => (string.IsNullOrEmpty(str1) ? ("0=0") : str1).Split('=').Equals(str)) ?
DIMVALUE.Where(str2 => (string.IsNullOrEmpty(str2) ? ("0=0") : str2).Split('=').Equals(str)).Select(str2 => str2.Split('=')).FirstOrDefault()
: "No Data").ToArray();
result = string.Join(",", varResult);
}```

नमस्ते,
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
• ### Re: C# or LINQ - How to search matching record and display specified format.

Shall i use below code or any other better solution for this.

``` List<string> rel = new List<string>();
for (int i = 0; i < OUTFORMULA.Length; i++)
{
string re = null;
for (int j = 0; j < DIMVALUE.Length; j++)
{
string[] str = DIMVALUE[j].Split('=').ToArray();
if (OUTFORMULA[i] == str) // Meeds to be handle "No Data" also here
{
break;
}
}

}
result = string.Join(",", rel.ToArray());```

• ### Re: C# or LINQ - How to search matching record and display specified format.

#### gani7787

Shall i use below code or any other better solution for this.

This can also be an another alternative. Here is a modified example to handle "No Data", with help of a boolean flag that will be set to false if any value found. Check out:

```protected void Button8_Click(object sender, EventArgs e)
{
string result = null;
string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };
string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };
List<string> rel = new List<string>();
for (int i = 0; i < OUTFORMULA.Length; i++)
{
bool noData = true;
for (int j = 0; j < DIMVALUE.Length; j++)
{
string[] str = DIMVALUE[j].Split('=').ToArray();
if (OUTFORMULA[i] == str) // Meeds to be handle "No Data" also here
{
noData = false;
break;
}
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.54296225,"math_prob":0.8469241,"size":6655,"snap":"2020-45-2020-50","text_gpt3_token_len":2164,"char_repetition_ratio":0.15997595,"word_repetition_ratio":0.45064807,"special_character_ratio":0.37009767,"punctuation_ratio":0.22381635,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98413134,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-27T20:25:36Z\",\"WARC-Record-ID\":\"<urn:uuid:1b7e845c-cd7f-469f-9b3c-91c8bd710e5e>\",\"Content-Length\":\"56033\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dfe9ce08-4af7-4ba1-8af6-3bf96a68f42b>\",\"WARC-Concurrent-To\":\"<urn:uuid:8f3a92e9-c0b0-4a9e-9a31-b393ca8d6fbe>\",\"WARC-IP-Address\":\"40.118.185.161\",\"WARC-Target-URI\":\"https://forums.asp.net/t/2125872.aspx?C+or+LINQ+How+to+search+matching+record+and+display+specified+format+\",\"WARC-Payload-Digest\":\"sha1:FEEL3ZUO5TXFP6FXD77H2KEU7UPMQ5KQ\",\"WARC-Block-Digest\":\"sha1:RN7EP2YVEKL7DC4O7MFFYHF6VPQBMA6Y\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107894759.37_warc_CC-MAIN-20201027195832-20201027225832-00536.warc.gz\"}"}