Click here to Skip to main content
15,914,395 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have structure like
C#
public class Categories {
  Public List<categorydescription> LisstOfcategories {get ; set;} 
} 

public class categoryDescription { 
  public string CategoryCode {get ;set;} 
}

And i have List of string which contains some categories like
C#
List<string> lst = new List<string>();

lst.Add("CategoryCode1"); 
lst.Add("CategoryCode2");
and List of Categories as
List<Categories > lstCategories Now how do i filter lstCategoriessuch that CategoryCode having same value defined in lst collection should not be selected in Linq C# ? How can i filter this ?


What I have tried:

i want to query through LINQ, i have tried following which does not compile lstCategories= lstCategories.Where(x => lst.Any(y => y != x.categoryDescription.ForEach(z => z.CategoryCode)));
Posted
Updated 10-May-17 21:34pm
v4
Comments
[no name] 10-May-17 8:02am    
so basically you are trying to populate only "Distint" codes here in you target object?
Member 10004269 10-May-17 8:50am    
No i just want to remove CategoryCode from lstCategories which are mentioned in lst
Member 10004269 10-May-17 8:52am    
No i just want to remove Categorycode from lstCategories which are mentioned in lst

First of all, your model (structure) is wrong!

I'd suggest to create Category object this way:
C#
public class Category
{
	private string sdescription = string.Empty;
	private string scode = string.Empty;

	public Category(string _description)
	{
		sdescription = _description;
	}
	
	public Category(string _description, string _code)
	{
		sdescription = _description;
		scode = _code;
	}
	
	public string Description
	{
		get { return sdescription; }
		set { sdescription = value; }
	}
	
	public string Code
	{
		get { return scode; }
		set { scode = value; }
	}
}


Now, your list could look like:
C#
List<Category> Categories = new List<Category>()
    {
        new Category("Books", "001"),
        new Category("Cars", "002"),
        new Category("Tools", "003")
    };

And another list of categories (CategoryCode):
C#
List<string> lst = new List<string>()
    {
        "Cars",
        "Dogs"
    };


Finall query:
C#
var CategoriesNotOnLst = Categories
    .Where(x => !lst.Any(z => x.Description==z))
    .ToList();


Result:
Description Code
Books       001 
Tools       003 
 
Share this answer
 
v4
Comments
Member 10004269 11-May-17 3:34am    
No I cant Change Existing Code structure

So i can give you another Explanation as follows

public class Zoo
{
public string zooName {get ; set;}

public List<animaldescriptions> animalDescriptions {get ; set;}
}

public class animalDescriptions

{
public string AnimalName {get ; set;}


}

Now we have List of Zoo as

List<zoo> ZooList = new List<zoo>();

var animaldescriptionsObj = new animalDescriptions { AnimalName = "Monkey"};
var animaldescriptionsObj2 = new animalDescriptions { AnimalName = "tiger"};


zoolist.add({ zooName = "Zoo1", List<animaldescriptions>{animaldescriptionsObj ,animaldescriptionsObj 2 }})

And we have other List of String as
List<string > lst = new List<string>();
lst.Add("Lion");
lst.Add("tiger");



Now i want linq Query so that AnimalNames defined in lst should not be selected from Zoolist ..so in result tiger animal name should be removed from Zoolist
You can achieve it by following way:
C#
lstCategories.Where(x => lst.Contains(x.CategoryCode));
 
Share this answer
 
Comments
Richard Deeming 10-May-17 14:19pm    
The question is confusing, but I think the OP wants it the other way round - return the categories that don't exist in lst.

You're also one level too high in the hierarchy: lstCategories is a List<Categories>, but your code expects it to be a List<categoryDescription>.
Maciej Los 10-May-17 14:20pm    
Seems, OP wants to get list of categories which not exists on string list...
So, your query should be: lstCategories.Where(x => !lst.Contains(x.CategoryCode));
+4!
Member 10004269 11-May-17 3:34am    
No I cant Change Existing Code structure

So i can give you another Explanation as follows

public class Zoo
{
public string zooName {get ; set;}

public List<animaldescriptions> animalDescriptions {get ; set;}
}

public class animalDescriptions

{
public string AnimalName {get ; set;}


}

Now we have List of Zoo as

List<zoo> ZooList = new List<zoo>();

var animaldescriptionsObj = new animalDescriptions { AnimalName = "Monkey"};
var animaldescriptionsObj2 = new animalDescriptions { AnimalName = "tiger"};


zoolist.add({ zooName = "Zoo1", List<animaldescriptions>{animaldescriptionsObj ,animaldescriptionsObj 2 }})

And we have other List of String as
List<string > lst = new List<string>();
lst.Add("Lion");
lst.Add("tiger");



Now i want linq Query so that AnimalNames defined in lst should not be selected from Zoolist ..so in result tiger animal name should be removed from Zoolist

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