We help IT Professionals succeed at work.

How do I use LINQ Contains(string[])

lulu50
lulu50 asked
on
Hi,

I'm working with linq and need your help please.

I have a string[] SelectedVersion that I pass from my view to the controller.

I want the linq to return query that contain the string SelectedVersion[]

list = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().ToList();

list = list.Where(x => x.Version.Contains(model.SelectedVersion));

model.SelectedVersion is a string array 
x.Version is a string 

Open in new window



the error says: " I cannot convert string [] to string "

how can I solve this syntax?
Comment
Watch Question

it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You need to use Any on your array of string in order to compare the elements with the version; e.g. -
using System;
using System.Linq;

namespace EE_Q29172573
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new[] { new { Version = "Apple", Color = "Red" }, new { Version = "Orange", Color = "Orange" }, new { Version = "Apple", Color = "Green" }, new { Version = "Pear", Color = "Green" }, new { Version = "Pineapple", Color = "Brown" } };
            var models = new[] { "Pineapple", "Apple" };

            foreach (var item in list.Where(x => models.Any(m => Equals(x.Version, m))))
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-
lulu50Web application

Author

Commented:
Hi saige,

Thank you for helping with this.

I'm confused and not sure how to do it.
let me post the whole code


    public List<RuleDetail> RuleDetailList(CABRSearchRules model)
        {
            List<RuleDetail> modelList = new List<RuleDetail>();

            IEnumerable<CABR_RuleDetail> list = new List<CABR_RuleDetail>();
            IEnumerable<CABR_HistoryRuleDetail> Historylist = new List<CABR_HistoryRuleDetail>();

            if (model.SelectedCurrentOrHistoryID == 1) //Current
            {
                list = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().ToList();

                // Status
                if (model.SelectedStatus != null)
                    list = list.Where(x => x.isActive == model.SelectedStatus);

                // Environment
                if (model.SelectedEnvironment != null)
                    list = list.Where(x => x.Environment == model.SelectedEnvironment);

                // Version
             [b]   if (model.SelectedVersion != null)
                {
                    list = list.Where(x => x.Version.Contains(model.SelectedVersion));
                    // Here I'm getting the error 
                   // Cannot convert string[] to string

                }
[/b]

            }

            var GetRuleset = _unitOfWorkCABusinessRules.RuleSetRepo.GetAll()
                .Where(x => x.IsActive == true).ToList();

            var GetRuleIdentifier = _unitOfWorkCABusinessRules.RuleIdentifierRepo.GetAll()
                .Where(x => x.IsActive == true).ToList();

            modelList = list.ToList().Select(x => new RuleDetail
            {
                Status = (Boolean)x.isActive,
                RuleSet = GetRuleset.Where(a => a.RuleSetId == x.RuleSetId).FirstOrDefault().RuleSet,
                RuleIdentifier = GetRuleIdentifier.Where(a => a.RuleIdentifierId == x.RuleIdentifierId).FirstOrDefault().RuleIdentifier,
                Versions = x.Version,
                Environments = x.Environment,
                CommitID = x.CommitID,
                CommitDescription = x.CommitDescription,
                RuleDescription = x.CommitDescription,
                ConditionAttributesAndValues = x.ConditionAttributesAndValues,
                ActionAttributesAndValues = x.ActionAttributesAndValues,
                CommentOrNotes = x.CommentOrNotes,
                MigrationDate = x.MigrationDate,
                CreatedDate = x.CreatedDate,
                SubmitterLastNameFirstName = x.SubmitterLastNameFirstName,
                CQTicket = x.CQTicket
            }).ToList();

            return modelList;

        }

Open in new window

Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Change line 23:
list = list.Where(x => model.SelectedVersion.Any(m => Equals(x.Version, m)));

Open in new window

-saige-
lulu50Web application

Author

Commented:
Saige!!

it works great!!!


I have some more questions but I have to open a new question for it.

one minute please
lulu50Web application

Author

Commented:
Thank you for helping with it

I have some more questions

I'll open a new question soon

please don't go anywhere. lol lol lol
I need your help!!!