Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

c# & linq

I have below codes with linq and it works. It return 1 LIst<string> from x.UI_Benefit.
However, I want to return two List<string> From new column call x.UI_Amount.
How can I return two list<string> from one ReadUI method?

Thanks

 public static List<string> ReadUI(string selectString)
        {
            Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext baseRateDataContext = new Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext();
            return (from x in baseRateDataContext.QT_AG_AC_ACP_BaseRates
                    where x.UI_Benefit == selectString
                    select x.UI_Benefit
                    ).ToList();
        }
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

A function can only have a single return value. You will have to create a class with two properties, each of which is a list. Populate the properties of that class in your method  and return that.
you can take help of objects in this case
the other way is you can try the sample below
say for example
public static List<List<string>> ReadUI(string selectString)
        {
	   List<List<string>> array_list = new List<List<string>>();
          array_list.Add(Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext baseRateDataContext = new 			 Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext();
	                 return (from x in baseRateDataContext.QT_AG_AC_ACP_BaseRates
             	         where x.UI_Benefit == selectString
                         select x.UI_Benefit).ToList());

// add the othere list element below
        }

Open in new window

hope this helps.
@Jitendra... My big concern there would be maintainance and readability over functionality :) but nice example of the power of LINQ
Avatar of ITsolutionWizard

ASKER

Jitendra Patil: Your code still return one column only (UI_Benefit),
I do not know what you mean by add the others list element below.
please show me in codes if possible.
I would go with something like this, notice that i introduce a class BenefitsData (you could use an anonymous return type if you like)
public static BenefitsData ReadUI(string selectString)
{
	BenefitsData data = new BenefitsData();
	var baseRateDataContext = new Repository.Rater.Life.Carriers.AmericanGeneral.ACPBaseRateDataContext();
	var query = (
			from x in baseRateDataContext.QT_AG_AC_ACP_BaseRates
			where x.UI_Benefit == selectString
		).ToList();

	data.Benefits = query.Select(x => x.UI_Benefit).Distinct().ToList();
	data.Amounts = query.Select(x => x.UI_Amount).Distinct().ToList();
}

public class BenefitsData
{
	public List<string> Amounts { get; set; }
	public List<string> Benefits { get; set; }
}

Open in new window

and to use
//to use
var result = ReadUI("myfilter");
result.Amounts
result.Benefits

Open in new window

IT Solutionwizard sorry for the late respone, as MlandaT described in the above example, this was what i suggested in my first comment.
you can take help of objects in this case

MlandaT great example above, maintainance and readability  wise its good. Thanks for the example.
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial